未在EnterpriseLibrary日志记录的自定义跟踪侦听器中设置Formatter

时间:2015-11-17 17:09:05

标签: c# logging enterprise-library

我已为EnterpriseLibrary日志记录块创建了自定义跟踪侦听器,但Formatter属性始终为null。

这是代码:

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="Console Trace Listener" 
           listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.SystemDiagnosticsTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
           formatter="Simple Formatter"
           type="test.TestTraceListener, test"
           traceOutputOptions="DateTime, Timestamp, ThreadId" />
    </listeners>
    <formatters>
      <add name="Simple 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="Console 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>

如果我理解正确,我的听众应该拥有&#34;简单格式化器&#34;我在配置文件中在Formatter属性中声明的格式化程序,但情况并非如此。

我错过了什么?

1 个答案:

答案 0 :(得分:0)

我使用企业库配置工具解决了问题(我可以按照此处的说明使其适用于VS 2015:Does Enterprise Library 6 work with Visual Studio 2013 and/or 2015?)。

问题是listenerDataType属性的值错误,侦听器的XML声明应该如下:

  <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" />