Formatting logging for the SystemDiagnosticsTraceListenerData-listener log block

I need to write to the console (or debug / trace). I am currently using the Ent Lib 4.1 registration unit. To log in to the console, I use SystemDiagnosticsTraceListenerData and System.Diagnostics.ConsoleTraceListener.

It logs to a thin console, but I cannot use the formatter for this type of lister and therefore cannot format the log entries to the desired format. All I need is a log message, with no additional information provided by default (which makes the logs less readable for my case).

Is there any configuration parameter that I am missing to enable formatting for SystemDiagnosticsTraceListener?

+4
source share
2 answers

One way to achieve this is to create a custom TraceListener. Then in app.config set listnerDataType to CustomTraceListenerData. This allows the ConsoleTraceListener to have formatted output. The output was formatted as expected. Without formatting, all values ​​were returned. Both custom TraceListener and app.config are included.

The code uses 5.0.505.0 instead of 4.1, as asked by the original question.

This code was taken from this site: java2s firstbricks "FirstBricks" EntLib "Registration" Extensions "ConsoleTraceListener.cs ( cache ). Comments were removed to make the code more readable in StackOverflow, and the default color was changed from white to gray .

namespace Eab.Logging { 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; [ConfigurationElementType(typeof(CustomTraceListenerData))] public class ConsoleTraceListener : CustomTraceListener { public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data) { if (data is LogEntry && Formatter != null) { LogEntry le = (LogEntry)data; WriteLine(Formatter.Format(le), le.Severity); } else { WriteLine(data.ToString()); } } public override void Write(string message) { Console.ForegroundColor = ConsoleColor.Gray; Console.Write(message); } public override void WriteLine(string message) { Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(message); } public void WriteLine(string message, TraceEventType severity) { ConsoleColor color; switch (severity) { case TraceEventType.Critical: case TraceEventType.Error: color = ConsoleColor.Red; break; case TraceEventType.Warning: color = ConsoleColor.Yellow; break; case TraceEventType.Information: color = ConsoleColor.Cyan; break; case TraceEventType.Verbose: default: color = ConsoleColor.Gray; break; } Console.ForegroundColor = color; Console.WriteLine(message); } } } 

  <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General"> <listeners> <!-- Namespace+class, applicationName --> <add type="Eab.Logging.ConsoleTraceListener, Eab.Logging" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" formatter="Simple Formatter" name="Console Listener" /> </listeners> <formatters> <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" template="{timestamp(local:[MM/dd/yyyy HH:mm:ss.fff])} : ({title}) {message}" name="Simple Formatter" /> </formatters> <categorySources> <add switchValue="All" name="General"> <listeners> <add name="Console Listener" /> </listeners> </add> </categorySources> <specialSources> <notProcessed switchValue="All" name="Unprocessed Category"> <listeners> <add name="Console Listener" /> </listeners> </notProcessed> <errors switchValue="All" name="Logging Errors &amp; Warnings"> <listeners> <add name="Console Listener" /> </listeners> </errors> </specialSources> </loggingConfiguration> 
+8
source

I don't know if this helps (since you are using LAB), but Ukadc.Diagnostics is a collection of System.Diagnostics extensions. One of the main additions (compared to System.Diagnostics) is the addition of the ability to format log output, similar to what can be done using Log4net, NLog, and LAB. You can even expand the formatting options by writing your own tokens. Tokens are objects called by custom TraceListeners provided by Ukadc.Diagnostics in order to receive information for registration (together with the journal message itself). For example, I wrote an object to calculate the delta time in milliseconds from the beginning of the process. If I include the corresponding token in the format operator, each log message will include this delta.

See these links:

Ukadc.Diagnostics on codeplex

Developer Blog

0
source

All Articles