未在EnterpriseLibrary日志记录的自定义跟踪侦听器中设置格式化程序



我为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>

如果我理解正确,我的侦听器应该有我在配置文件中声明的"Simple Formatter"格式化程序的Formatter属性,但事实并非如此。

我错过了什么?

我用企业库配置工具解决了这个问题(我可以按照以下说明使它适用于VS 2015:企业库6适用于Visual Studio 2013和/或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" />

最新更新