2015-11-17 5 views
0

J'ai créé un écouteur de trace personnalisé pour le bloc de journalisation EnterpriseLibrary, mais la propriété Formatter est toujours NULL.Formatter non défini dans l'écouteur de trace personnalisé pour la journalisation EnterpriseLibrary

Voici le code:

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

namespace test { 

    [ConfigurationElementType(typeof(CustomTraceListenerData))] 
    public class TestTraceListener : CustomTraceListener { 

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

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

     public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data) { 
      LogEntry entry = data as LogEntry; 
      if (entry != null) { 
       if (Formatter != null) { 
        string formatted = Formatter.Format(entry); 
        WriteLine(formatted); 
       } else { 
        WriteLine(entry.Message); 
       } 
      } else { 
       base.TraceData(eventCache, source, eventType, id, data); 
      } 
     } 

    } 

    class Program { 
     static void Main(string[] args) { 
      Logger.SetLogWriter(new LogWriterFactory().Create()); 
      LogEntry entry = new LogEntry("This is a test", "General", 0, 0, TraceEventType.Information, null, null); 
      Logger.Write(entry); 
     } 
    } 
} 

Et ceci est le fichier de configuration:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
    <section name="loggingConfiguration" 
      type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
      requirePermission="true" /> 
    </configSections> 
    <loggingConfiguration name="logging" tracingEnabled="true" defaultCategory="General"> 
    <listeners> 
     <add name="Console Trace Listener" 
      listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.SystemDiagnosticsTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
      formatter="Simple Formatter" 
      type="test.TestTraceListener, test" 
      traceOutputOptions="DateTime, Timestamp, ThreadId" /> 
    </listeners> 
    <formatters> 
     <add name="Simple Formatter" 
      type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
      template="{timestamp(local:dd/MM/yy HH:mm:ss.fff)} [{severity}]: {message}" /> 
    </formatters> 
    <categorySources> 
     <add switchValue="Information" name="General"> 
     <listeners> 
      <add name="Console Trace Listener" /> 
     </listeners> 
     </add> 
    </categorySources> 
    <specialSources> 
     <allEvents switchValue="All" name="All Events" /> 
     <notProcessed switchValue="All" name="Unprocessed Category" /> 
     <errors switchValue="Warning" name="Logging Errors &amp; Warnings" /> 
    </specialSources> 
    </loggingConfiguration> 
    <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" /> 
    </startup> 

</configuration> 

Si je comprends bien, mon auditeur doit avoir le formatter "Simple Formatter" que je déclarais dans la fichier de configuration dans sa propriété Formatter, mais ce n'est pas le cas.

Qu'est-ce qui me manque?

Répondre

0

J'ai résolu le problème avec l'outil de configuration de bibliothèque d'entreprise (je pourrais le faire fonctionner pour VS 2015 en suivant les instructions ici: Does Enterprise Library 6 work with Visual Studio 2013 and/or 2015?).

Le problème était une mauvaise valeur de l'attribut listenerDataType, la déclaration XML de l'auditeur aurait dû être le suivant:

<add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
     type="test.TestTraceListener, test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" 
     name="Console Trace Listener" 
     formatter="Simple Formatter" />