2009-06-18 8 views
11

Voglio implementare la registrazione con EntLib Logging e collegare due TraceListeners per la categoria "Debug". Uno scriverà quei messaggi in un file e altri li invieranno all'output di trace del sistema nello stesso modo in cui fa Debug.Write (in modo che io possa controllarli con Sysinternals DbgView), ma non riesco a trovare come impostare questo secondo listener con formattatore che Ho bisogno. Tutto ciò di cui ho veramente bisogno è solo un messaggio, ma emette un sacco di cose, come EventId, Priorità, ecc. Come posso tagliare tutte queste cose?Come scrivere solo un messaggio per eseguire il debug dell'output con Enterprise Library Logging?

risposta

15

ho trovato un bel procedura dettagliata su MSDN: Creating a Custom Trace Listener

Fa esattamente quello che mi serve. Ecco un codice completo ho finito con:

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

namespace Common.Utils 
{ 
    [ConfigurationElementType(typeof(CustomTraceListenerData))] 
    public class FormattedDebugWriterTraceListener : 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) 
     { 
      Debug.Write(message); 
     } 

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

    } 
} 

file di configurazione:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <configSections> 
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> 
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> 
    </configSections> 
    <loggingConfiguration name="Logging Application Block" tracingEnabled="true" 
    defaultCategory="General" logWarningsWhenNoCategoriesMatch="true"> 
    <listeners> 
     <add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 
     traceOutputOptions="None" type="Common.Utils.FormattedDebugWriterTraceListener, Common.Utils" 
     name="FormattedDebugWriterTraceListener" initializeData="" formatter="SimpleMessageFormatter" /> 
     <add fileName="log\Debugging.log" rollSizeKB="0" timeStampPattern="yyyy-MM-dd" 
     rollFileExistsBehavior="Overwrite" rollInterval="Week" formatter="GeneralTextFormatter" 
     header="----------------------------------------" footer="----------------------------------------" 
     listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 
     traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 
     name="RollingFlatFileTraceListener" /> 
    </listeners> 
    <formatters> 
     <add template="{message}&#xD;&#xA;" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 
     name="SimpleMessageFormatter" /> 
     <add template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}" 
     type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 
     name="GeneralTextFormatter" /> 
    </formatters> 
    <categorySources> 
     <add switchValue="All" name="Debugging"> 
     <listeners> 
      <add name="FormattedDebugWriterTraceListener" /> 
      <add name="RollingFlatFileTraceListener" /> 
     </listeners> 
     </add> 
     <add switchValue="All" name="General" /> 
    </categorySources> 
    <specialSources> 
     <allEvents switchValue="All" name="All Events" /> 
     <notProcessed switchValue="All" name="Unprocessed Category" /> 
     <errors switchValue="All" name="Logging Errors &amp; Warnings" /> 
    </specialSources> 
    </loggingConfiguration> 
</configuration> 

E l'utilizzo è questa:

Debug.Write("Debug.Write test"); 
Logger.Write("EntLib test", "Debugging"); 

Entrambi finiscono in uscita di debug facilmente rintracciabili da DbgView.

+0

Grazie mille per aver incluso l'XML - mi ha aiutato molto con qualcosa che non riuscivo a capire dalla procedura dettagliata! (Non vedo dove menzionano l'impostazione dell'attributo del tipo di dati listener, e mi stava facendo impazzire) – GrahamMc

0

Nella configurazione EntLib per l'app si specifica quale Formatter si desidera utilizzare. Il formattatore predefinito include tutte queste informazioni. Per rimuovere le informazioni che non ti interessano puoi rimuoverle dal TextFormatter che stai usando o creare un nuovo formattatore di testo contenente i campi che desideri e cambiare "Debug" per usare il tuo nuovo formattatore.

+2

Questo è esattamente ciò che ho fatto, ma sembra che DefaultTraceListener non supporti formattatore. – bychkov

Problemi correlati