entlib无效的TraceListenerData类型

时间:2011-09-02 11:21:20

标签: c# logging configuration dependency-injection enterprise-library

我为Enterprise Library 5 Logging Block创建了一个自定义侦听器,它由配置编辑器识别,但会引发运行时配置异常:

尝试使用自定义记录器

    static IUnityContainer _container;
    static LogWriter _writer;
    static IServiceLocator _locator;

    public static void Inf(string message)
    {
        if (_container == null)
        {
            // Create the container
            _container = new UnityContainer();
            // Configurator will read Enterprise Library configuration
            // and set up the container
            var configurator = new UnityContainerConfigurator(_container);
            // Configuration source holds the new configuration we want to use
            // load this in your own code
            IConfigurationSource configSource = new SystemConfigurationSource(true);
            // Configure the container
            EnterpriseLibraryContainer.ConfigureContainer(configurator, configSource);
            // Wrap in ServiceLocator
            _locator = new UnityServiceLocator(_container);
        }
        if (_writer == null)
        {
            _writer = _locator.GetInstance<LogWriter>();
        }
        if (_writer != null && _container != null)
        {
            LogEntry log = new LogEntry();
            log.Message = message;
            log.Categories.Add("Information");
            log.Severity = TraceEventType.Information;
            _writer.Write(log);
        }
    }

在dll中拥有TraceListener

using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Formatters;

namespace CustomLogger
{
    [ConfigurationElementType(typeof(LoggerCustomData))]
    public class LoggerCustom : TraceListener //CustomTraceListener
    {
        readonly ILogFormatter _formatter;

        public LoggerCustom()
            : this(string.Empty, null)
        {
        }

        public LoggerCustom(string name)
            : this(name, null)
        {
        }

        public LoggerCustom(string name, ILogFormatter formatter)
            : base(name)
        {
            this._formatter = formatter;
        }

        public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
        {
            if ((Filter == null) || Filter.ShouldTrace(eventCache, source, eventType, id, null, null, data, null))
            {
                if (data is LogEntry)
                {
                    if (_formatter != null)
                    {
                        WriteLine(_formatter.Format(data as LogEntry));
                    } else
                    {
                        base.TraceData(eventCache, source, eventType, id, data);
                    }
                } else
                {
                    base.TraceData(eventCache, source, eventType, id, data);
                }
            }
        }

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

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

自定义TraceListenerData在dll

using System;
using System.Configuration;
using System.Diagnostics;
using System.Linq.Expressions;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Common.Properties;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.Formatters;

namespace CustomLogger
{
    [ResourceDisplayName(typeof(Resources), "CustomLoggerDataDisplayName")]
    [ResourceDescription(typeof(Resources), "CustomLoggerDataDescription")]
    public class LoggerCustomData : TraceListenerData
    {
        private const string FormatterNameProperty = "formatter";

        public LoggerCustomData()
            : this("unnamed", null, TraceOptions.None)
        {
        }

        public LoggerCustomData(string name)
            : this(name, null, TraceOptions.None)
        {
        }

        public LoggerCustomData(string name, string formatterName)
            : this(name, formatterName, TraceOptions.None)
        {
        }

        protected LoggerCustomData(string name, string formatterName, TraceOptions traceOutputOptions)
            : base(name, typeof(LoggerCustom), traceOutputOptions, SourceLevels.All)
        {
            ListenerDataType = typeof(LoggerCustomData);
            Formatter = formatterName;
        }

        [ConfigurationProperty(FormatterNameProperty, IsRequired = false),
        Reference(typeof(NameTypeConfigurationElementCollection<FormatterData, CustomFormatterData>), typeof(FormatterData)),
        ResourceDisplayName(typeof(Resources), "CustomLoggerDataFormatterDisplayName"),
        ResourceDescription(typeof(Resources), "CustomLoggerDataFormatterDescription")]
        public string Formatter
        {
            get { return (string)base[FormatterNameProperty]; }
            set { base[FormatterNameProperty] = value; }
        }

        protected override Expression<Func<TraceListener>> GetCreationExpression()
        {
            return () =>
                new LoggerCustom(Name,
                    Container.ResolvedIfNotNull<ILogFormatter>(Formatter));
        }
    }
}

由ConfEditor生成的app.config添加

  <add name="CustomLoggerDataDisplayName" type="CustomLogger.LoggerCustom, CustomLogger, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
    listenerDataType="CustomLogger.LoggerCustomData, CustomLogger, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
    traceOutputOptions="None" filter="All" formatter="Text Formatter Plain" />
<。H2> .ConfigureContainer(configurator,configSource)的异常

配置中的TraceListenerData类型无效:listenerDataType =“CustomLogger.LoggerCustomData,CustomLogger,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null”'。

为什么不起作用? :( :(

P.S。这篇文章是entlib CustomTraceListener unresolved

的演变

1 个答案:

答案 0 :(得分:1)

在我的情况下,defaultCategory =“ Important”在配置第一行中我已更改为defaultCategory =“”,然后工作正常。