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
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}
" 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}
Message: {message}
Category: {category}
Priority: {priority}
EventId: {eventid}
Severity: {severity}
Title:{title}
Machine: {machine}
Application Domain: {appDomain}
Process Id: {processId}
Process Name: {processName}
Win32 Thread Id: {win32ThreadId}
Thread Name: {threadName}
Extended Properties: {dictionary({key} - {value}
)}"
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 & 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.
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.
Questo è esattamente ciò che ho fatto, ma sembra che DefaultTraceListener non supporti formattatore. – bychkov
- 1. Come utilizzare Exception Manager Enterprise Library 6.0
- 2. Enterprise Library Validation Application Block e Internazionalizzazione
- 3. pylint messaggio: logging-format-interpolazione
- 4. Vantaggi e svantaggi dell'utilizzo di Enterprise Library
- 5. Diverse versioni di Enterprise Library in un unico processo
- 6. Python Library/Framework per scrivere applicazioni P2P
- 7. Enterprise Library 5.0 - Registrazione applicazioni - DateTime non corrette
- 8. come eseguire il debug con xUnit?
- 9. Come eseguire il debug WebSockets con Wireshark
- 10. Come eseguire il debug di un deadlock?
- 11. Posso scrivere nel log della console per eseguire il debug di un'applicazione Web con C#
- 12. Come eseguire il debug del debug?
- 13. Come eseguire il debug di un programma compilato con 'make'?
- 14. ResourceNotFoundException come eseguire il debug?
- 15. Come posso utilizzare xdebug per eseguire il debug di un solo host virtuale?
- 16. Come eseguire il debug dell'applet cinnamon?
- 17. Come si usa alert() per un messaggio di debug lungo?
- 18. Come scrivere un metodo/messaggio con più parametri?
- 19. Come limitare un flusso Akka per eseguire e inviare un messaggio solo una volta al secondo?
- 20. Abilita il logging Django anche se DEBUG è True
- 21. Come eseguire il debug di un salvaschermo in OS X
- 22. COME USARE Pycharm per eseguire il debug di script Python?
- 23. Come eseguire il debug di un pacchetto Python in PyCharm
- 24. Registrazione da più processi allo stesso file utilizzando Enterprise Library 4.1
- 25. messaggio gdb strano quando il debug programma
- 26. Come eseguire il debug di CSS con Firebug per un elemento che appare solo quando si fa clic su?
- 27. Come eseguire il debug (passaggio in) BinaryFormatter.Deserialize()?
- 28. Come eseguire il debug del codice ruby?
- 29. Come eseguire il debug di un'applicazione Delphi con output reindirizzato
- 30. Node.js: come eseguire il debug in remoto?
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