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!
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);
            }
        }
    }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With