Contrary to the answers here, you DON'T need to worry about encoding if the bytes don't need to be interpreted!
Like you mentioned, your goal is, simply, to "get what bytes the string has been stored in".
(And, of course, to be able to re-construct the string from the bytes.)
For those goals, I honestly do not understand why people keep telling you that you need the encodings. You certainly do NOT need to worry about encodings for this.
Just do this instead:
static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
// Do NOT use on arbitrary bytes; only use on GetBytes's output on the SAME system
static string GetString(byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}
As long as your program (or other programs) don't try to interpret the bytes somehow, which you obviously didn't mention you intend to do, then there is nothing wrong with this approach! Worrying about encodings just makes your life more complicated for no real reason.
Additional benefit to this approach: It doesn't matter if the string contains invalid characters, because you can still get the data and reconstruct the original string anyway!
It will be encoded and decoded just the same, because you are just looking at the bytes.
If you used a specific encoding, though, it would've given you trouble with encoding/decoding invalid characters.
As empi says, we'll need to see your config file to tell for sure. However, I'll bet you have the Append
property set to false. From the log4net docs:
If the value is set to false then the
file will be overwritten, if it is set
to true then the file will be appended
to.
Try adding this to your RollingFileAppender config:
<appendToFile value="true" />
EDIT: Looking at your posted config file, this line looks odd:
<param name="StaticLogFileName" value="true" />
...which is documented as:
Gets or sets a value indicating
whether to always log to the same
file.
true if always should be logged to the
same file, otherwise false.
That sounds like it matches what you're seeing. Try removing that line, or setting it to false.
Best Answer
The value for this pattern will be in the
File
property of aFileAppender
instance that's part of your logging repository.Getting at the appender can be done in a couple of different ways.
ILogger
instance (e.g. what you get fromLogManager.GetLogger()
) then there's anAppenders
property on it. This will give you any appenders associated with that specific level of the logging heirarchy.LogManager.GetRepository()
to get aHeirarchy
object that contains the entire logger and appender heirarchy. The method expects an assembly, so passGetCallingAssembly()
to get the default one. This class has aGetAppenders()
method that returns all configured appenders, though it won't tell you which ones are associated with which loggers.From there, just look through looking for an appender of the correct type (e.g.
FileAppender
orRollingFileAppender
), then read it'sFile
property.