11

I am kind of stuck with my searches concerning EnterpriseLibrary.Logging. I have a listener and formatter set up like this:

<add name="NormalLogListener"
     type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging"
     listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging"
     fileName="logs/MVC22.log" 
     footer="" 
     formatter="ShortLogFormatter" 
     header="" 
     rollInterval="Day" 
     timeStampPattern="yyyy-MM-dd" 
     maxArchivedFiles="14" />

...

<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging" 
     template="{timestamp(local)} - {severity} - {category} - {message}"
     name="ShortLogFormatter" />

I use this in multiple projects and it is working fine.

Except for one thing, I want EnterpriseLibrary to create my log file with UTF-8-encoding (I get ANSI files per default), but unfortunately I have no clue how to do that.

I have special characters in strings that I want to be able to log into my file (such as umlauts); I see the logging works fine, when I convert my file to UTF-8 and let it be used further, but I really want to have it created that way.

Can this be done in the xml configuration or somewhere else?

Thanks for any help in advance!

pnuts
  • 58,317
  • 11
  • 87
  • 139
DrCopyPaste
  • 4,023
  • 1
  • 22
  • 57

1 Answers1

4

Out of the box, I don't believe that the EnterpriseLibrary.Logging application block supports output to utf-8. It appears to only output to the default ANSI. That being said, you could always write your own TraceListener that would output to utf-8.

Untested code. I wrote it quick and dirty. Watch the hard coded file path:

using System;
using System.Text;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

namespace YourNamespace
{
    [ConfigurationElementType(typeof(CustomTraceListenerData))]
    class UTF8Logging:CustomTraceListener
    {
        public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
        {
            if (data is LogEntry && this.Formatter != null)
            {
                this.WriteLine(this.Formatter.Format(data as LogEntry));
            }
            else
            {
                this.WriteLine(data.ToString());
            }
        }

        public override void Write(string message)
        {
            this.WriteLine(message);
        }

        public override void WriteLine(string message)
        {
            string fileName = @"C:\Your.log";
            using (StreamWriter sw = new StreamWriter(File.Exists(fileName) ? System.IO.File.Open(fileName, FileMode.Append) : System.IO.File.Create(fileName), Encoding.UTF8))
            {
                Byte[] logMessage = new UTF8Encoding(true).GetBytes(message);
                sw.Write(logMessage,0,logMessage.Length);
            }
        }
    }
}
TimWagaman
  • 980
  • 1
  • 10
  • 31
  • Yeah, I fear that too, but I have not given up hope yet ;) Maybe you got a good example for that? – DrCopyPaste Mar 26 '13 at 12:59
  • Version of EnterpriseLibrary? – TimWagaman Mar 26 '13 at 13:08
  • I will test that, but configuring the path to my logfile now in code seems not so nice, can't this grab the path from my TraceListener declaration in xml? – DrCopyPaste Mar 26 '13 at 14:21
  • Agreed. Like I said, it was quick and dirty. I didn't see a sample of how to configure the path in the TraceListener declaration, but I know you can. – TimWagaman Mar 26 '13 at 14:25
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27008/discussion-between-timwagaman-and-drcopypaste) – TimWagaman Mar 27 '13 at 12:22
  • enforcing utf-8 using UTF8Encofing.GetBytes and simple System.IO.File did not work for me; though using a StreamWriter that again uses a System.IO.File instance I could pass an extra encoding parameter there, I edit this into your answer and award you the bounty, since it poked me into the right direction it seems ;) – DrCopyPaste Apr 03 '13 at 10:45
  • specifying the encoding by converting to a byte array is no longer necessary if StreamWriter was initialized with the right encoding – DrCopyPaste Apr 04 '13 at 14:50