Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting logging for SystemDiagnosticsTraceListenerData listener of Logging Application Block

I need to perform logging to console (or debug/trace). Currently I'm using Ent Lib 4.1 Logging Application Block. To log to console I use SystemDiagnosticsTraceListenerData and System.Diagnostics.ConsoleTraceListener.

It performs logging to console fine, but I'm not able to use formatter for this lister type and thus can't format log entries to desired format. What I need is just log message, without having all additional info tha is provided by default (which makes logs less readable for my case).

Is there any configuration option I'm missing to enable formatting for SystemDiagnosticsTraceListener?

like image 526
Andrii Avatar asked Feb 27 '23 12:02

Andrii


1 Answers

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

The code is using 5.0.505.0 instead of 4.1 as posed by the original question.

This code was taken from this site: java2s firstbricks » FirstBricks » EntLib » Logging » Extensions » ConsoleTraceListener.cs (cache). Comments were removed to make code easier to read on 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>
like image 150
jhamm Avatar answered Apr 28 '23 11:04

jhamm