Flat file trace listener output format

Topics: Logging Application Block
Mar 17, 2011 at 3:24 AM

I’m using EL 5.0 logging block without issues except the log file generated by the flat file trace listener is in ANSI format. Any way to set it to be UTF-8/Unicode?

Thanks,
Dioni

Mar 17, 2011 at 4:12 AM

There's no configurable way of doing this, I think.  I came across this item and it says the default encoding is UTF-8 for the TextWriterTraceListener which is eventually the FlatFileTraceListener's baseclass.  Anyway, if you want to change the encoding, you would need to implement a custom trace lsitener which will eventually call the constructor of the TextWriterTraceListener which passes a StreamWriter where the encoding can be specified.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Mar 17, 2011 at 4:40 PM

Thanks, Sarah. I was hoping there would be a more elegant way, but in the mean time I found a workaround for this, in case someone is interested:

You can create the log file in your code with the UTF preamble and then let the EF logger append text to it, which will solve the issue. I just tried the code below and it works just fine. I do not want/need to touch the file if it already exists in an scenario in which the logger is appending to a previously created log.

        public Logger(string logFile, int verbosity)
        {
            if (!File.Exists(logFile))
            {
                File.WriteAllText(logFile, string.Empty, Encoding.UTF8);
            }
 
            // keep going initializing/configuring the EF logger
        }