本文介绍了entlib无效的TraceListenerData类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've created a custom listener for Enterprise Library 5 Logging Block, which is recognized by the Configuration Editor, but throws a run-time configuration exception:

    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);
        }
    }
}

dll中的自定义TraceListenerData

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添加

app.config addition generated by ConfEditor

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

.ConfigureContainer(configurator,configSource)处的异常

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

Exception at .ConfigureContainer(configurator, configSource)

Invalid TraceListenerData type in configuration 'listenerDataType="CustomLogger.LoggerCustomData, CustomLogger, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"'.

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

Why does it not work ? :( :(

p.s.这篇文章是 entlib CustomTraceListener未解决

p.s. this post is an evolution of entlib CustomTraceListener unresolved

推荐答案

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

In my case defaultCategory="Important" I have changed to defaultCategory="" in the Configuration first line, then worked fine.

这篇关于entlib无效的TraceListenerData类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 19:48