Question

I am trying to customize Enterprise library logging by adding a formatter inside the app.config file. The problem is that the logging API dumps additional information along with the item I specified in the formatter.

Here is the App.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="LoggingBlock" tracingEnabled="true"
    defaultCategory="General">
    <listeners>
      <add name="RollingFile" 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="DataLog.log" formatter="SimpleFormatter" rollFileExistsBehavior="Increment"
        rollInterval="Day" rollSizeKB="10000" />
    </listeners>
    <formatters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        template="Message: {message}{newline}&#xA;" name="SimpleFormatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="RollingFile" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings" />
    </specialSources>
  </loggingConfiguration>
</configuration>

I expected it to append only one line to the log file:

Message: Sample log text

but it adds the following:

Message: There is no explicit mapping for the categories 'Info'. The log entry was: Timestamp: 31/10/2013 8:59:01 AM

Message: Sample log text

Category: Info

Priority: 2

EventId: 1

Severity: Information

Title:

Machine: SERVER1

App Domain: SAMPLE.exe

ProcessId: 1104

Process Name:

Thread Name:

Win32 ThreadId:9644

Extended Properties:

How do I get rid of the additional lines?

The API used is:

Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write(message, category, priority)
Was it helpful?

Solution

Finally figured out that you can do so by overriding the formatter type in the app.config file like so:

<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        template="[{timestamp(local)}] {category},{machine},{message}"
        name="Text Formatter" />

The difficulty was in finding the template keywords for the data I was interested in adding to the log.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top