2013-03-19 15 views
11

Trochę utknąłem z moich wyszukiwań dotyczących EnterpriseLibrary.Logging. Mam słuchacza i formatowania skonfigurować tak:Jak zalogować się przy użyciu UTF-8 za pomocą EnterpriseLibrary.Logging

<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" /> 

Używam tego w wielu projektach i to działa dobrze.

Z wyjątkiem jednej rzeczy, chcę EnterpriseLibrary, aby utworzyć mój plik dziennika z kodowaniem UTF-8 (domyślnie otrzymuję pliki ANSI), ale niestety nie mam pojęcia, jak to zrobić.

Mam specjalne znaki w ciągach, które chcę móc zalogować się do mojego pliku (takie jak umlauty); Widzę, że rejestrowanie działa dobrze, kiedy konwertuję plik na UTF-8 i pozwalam go dalej wykorzystywać, ale naprawdę chcę, aby został on utworzony w ten sposób.

Czy można to zrobić w konfiguracji xml lub gdzieś indziej?

Dzięki za pomoc z góry!

Odpowiedz

4

Po wyjęciu z pudełka, nie wierzę, że blok aplikacji EnterpriseLibrary.Logging obsługuje wyjście do utf-8. Wygląda na to, że wyprowadza się tylko do domyślnego ANSI. Biorąc to pod uwagę, zawsze możesz napisać własny TraceListener, który wyświetli wynik utf-8.

Kod niewyszukany. Napisałem to szybko i brudno. Zobacz ścieżkę z zakodowanym plikiem:

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); 
      } 
     } 
    } 
} 
+0

Tak, ja też się tego boję, ale jeszcze nie straciłem nadziei;) Może masz na to dobry przykład? – DrCopyPaste

+0

Wersja EnterpriseLibrary? – TimWagaman

+0

Otrzymałem wersję 5 tutaj – DrCopyPaste

Powiązane problemy