2009-06-18 4 views
11

Je veux implémenter la journalisation avec EntLib Logging et raccorder deux TraceListeners pour la catégorie "Debugging". On écrira ces messages dans un fichier et d'autres les sortiront vers la sortie de trace du système de la même manière que Debug.Write (pour que je puisse les surveiller avec Sysinternals DbgView), mais je ne peux pas trouver comment configurer ce second écouteur avec le formateur J'ai besoin. Tout ce dont j'ai vraiment besoin, c'est d'un simple message, mais il sort tout un tas de choses, comme EventId, Priority, etc. Comment puis-je couper tout ça?Comment écrire un message pour déboguer la sortie avec Enterprise Library Logging?

Répondre

15

J'ai trouvé une belle visite virtuelle sur le site MSDN: Creating a Custom Trace Listener

Il fait exactement ce que je dois. Voici un code complet j'ai fini avec:

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

    } 
} 

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=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> 

Et l'utilisation va comme ceci:

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

clôturons en sortie de débogage facilement traçable par DbgView.

+0

Merci beaucoup d'avoir inclus le XML - cela m'a beaucoup aidé avec quelque chose que je n'ai pas pu trouver dans la procédure pas à pas! (Je ne vois pas où ils mentionnent la définition de l'attribut type de données de l'écouteur, et cela me rendait fou) – GrahamMc

0

Dans la configuration EntLib de votre application, vous spécifiez le formateur que vous souhaitez utiliser. Le formateur par défaut inclut toutes ces informations. Pour supprimer les informations qui ne vous intéressent pas, supprimez-les du TextFormatter que vous utilisez actuellement ou créez un nouveau formateur de texte contenant les champs souhaités et modifiez "Debugging" pour utiliser votre nouveau formateur.

+2

C'est exactement ce que j'ai fait, mais on dirait que DefaultTraceListener ne supporte pas le formatter. – bychkov

Questions connexes