Formatter not set in custom trace listener

Topics: Logging Application Block
Nov 18, 2015 at 7:56 AM
I have created a custom trace listener for the EnterpriseLibrary logging block, but the Formatter property is always null, even if I have set a formatter from the configuration file.

Am I missing something?

Here is my code and my configuration file:
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
using System;
using System.Diagnostics;

namespace test {

    [ConfigurationElementType(typeof(CustomTraceListenerData))]
    public class TestTraceListener : CustomTraceListener {

        public override void Write(string message) {
            Console.Write(message);
        }

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

        public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data) {
            LogEntry entry = data as LogEntry;
            if (entry != null) {
                if (Formatter != null) {
                    string formatted = Formatter.Format(entry);
                    WriteLine(formatted);
                } else {
                    WriteLine(entry.Message);
                }
            } else {
                base.TraceData(eventCache, source, eventType, id, data);
            }
        }

    }

    class Program {
        static void Main(string[] args) {
            Logger.SetLogWriter(new LogWriterFactory().Create());
            LogEntry entry = new LogEntry("This is a test", "General", 0, 0, TraceEventType.Information, null, null);
            Logger.Write(entry);
        }
    }
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="loggingConfiguration"
              type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              requirePermission="true" />
  </configSections>
  <loggingConfiguration name="logging" tracingEnabled="true" defaultCategory="General">
    <listeners>
      <add name="Test Trace Listener" 
            listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.SystemDiagnosticsTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            formatter="Test Formatter"
            type="test.TestTraceListener, test"
            traceOutputOptions="DateTime, Timestamp, ThreadId" />
    </listeners>
    <formatters>
      <add name="Test Formatter"
           type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
           template="{timestamp(local:dd/MM/yy HH:mm:ss.fff)} [{severity}]: {message}" />
    </formatters>
    <categorySources>
      <add switchValue="Information" name="General">
        <listeners>
          <add name="Test Trace Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="Warning" name="Logging Errors &amp; Warnings" />
    </specialSources>
  </loggingConfiguration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
  </startup>

</configuration>
Nov 18, 2015 at 8:28 AM
I solved the problem with the Enterprise Library Configuration Tool (I could make it work for VS 2015 following the instructions here: http://stackoverflow.com/questions/24309323/does-enterprise-library-6-work-with-visual-studio-2013-and-or-2015).

The problem was a wrong value of the listenerDataType attribute, the XML declaration of the listener should have been the following:
  <add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
       type="test.TestTraceListener, test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
       name="Console Trace Listener"
       formatter="Simple Formatter" />