Search code examples
c#enterprise-library

Why its not following template from formatter


Here is my config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    </configSections>
  <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General" >
    <listeners>
      <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                source="Enterprise Library Logging" formatter="Text Formatter"
                log="" machineName="." traceOutputOptions="None" />
      <add name="Rolling Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                fileName="F:\MyLogFile.log" footer="" header="" rollInterval="Hour"
                traceOutputOptions="None" formatter="Text Formatter" />
    </listeners>
    <formatters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                template="{timestamp} {severity} {message} "
                name="Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Rolling Flat File Trace Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Rolling Flat File Trace Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
</configuration>

when I use the code below

 Logger.Write("hello world", "", 0, 0, TraceEventType.Information);

I get following in the log file

3/4/2011 7:40:26 PM Error There is no explicit mapping for the categories ''. The log entry was:
Timestamp: 3/4/2011 7:40:26 PM
Message: Hello World
Category:
Priority: 0
EventId: 0
Severity: Information
Title:
Machine: MyPC
App Domain: ConsoleApplication1.vshost.exe
ProcessId: 8912
Process Name: C:\ConsoleApplication\bin\Debug\ConsoleApplication.vshost.exe
Thread Name:
Win32 ThreadId:5496
Extended Properties:

What am I doing wrong that make it not respecting the template="{timestamp} {severity} {message} " that I have defined in formatter.


Solution

  • You've defined your category with the name "General" but when you log you are using the category "".

    Logger.Write("hello world", "", 0, 0, TraceEventType.Information);
    

    So your LogEntry is not being processed by your "General" category but instead by the specialSource notProcessed. That's what the message "There is no explicit mapping for the categories" is trying to tell you.

    To use your category pass the name in to the Write method:

    Logger.Write("hello world", "General", 0, 0, TraceEventType.Information);
    

    or don't specify a category since "General" is defined as the defaultCategory.