entlib CustomTraceListener unresolved

本文关键字:unresolved CustomTraceListener entlib | 更新日期: 2023-09-27 18:07:04

我是企业库的新手(5,带有可选的更新),我试图使用我自己的CustomTraceListener与日志块。

日志与提供的平面文件侦听器一起工作,但我似乎无法将其扩展到我的自定义类。

记录FlatFileListener,但不记录我的CustomListener:

    static UnityContainer _container;
    static LogWriter _writer;
    internal static void Inf(this string message)
    {
        if (_container == null)
        {
            _container = new UnityContainer();
            _container.AddNewExtension<EnterpriseLibraryCoreExtension>();
        }
        if (_writer == null && _container != null)
        {
            _writer = _container.Resolve<LogWriter>();
            LogEntry log = new LogEntry();
            log.Message = message;
            log.Categories.Add("Information");
            log.Severity = TraceEventType.Information;
            _writer.Write(log);
        }
    }

我的CustomListener基于bychkov:

namespace Test.Catch
{
    [ConfigurationElementType(typeof(CustomTraceListenerData))]
    public class LoggerCustom : CustomTraceListener
    {
        public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
        {
            if (data is LogEntry && this.Formatter != null)
            {
                this.WriteLine(this.Formatter.Format(data as LogEntry));
            } else
            {
                this.WriteLine(data.ToString());
            }
        }
        public override void Write(string message)
        {
            Trace.Write(message);
        }
        public override void WriteLine(string message)
        {
            Trace.WriteLine(message);
        }
    }
}

首先,我尝试使用配置编辑器添加customtracelistener,但配置编辑器没有列出/检测任何customtracelistener,我怀疑这是问题的一部分或全部?

因此,我基于bychkov再次添加了以下内容:

应用程序。手动添加监听器

  <add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
  type="Test.Catch.LoggerCustom, Test.Catch" traceOutputOptions="None"
  name="Custom Trace Listener" initializeData="" formatter="Text Formatter Plain" />

现在,当我调用Inf("logme")时,添加了上面的app.config,然后在_container.AddNewExtension()抛出一个异常:类型"Test.Catch"。LoggerCustom、测试。Catch'无法解决。

考虑到我不理解底层机制,我怎么能用我的自定义侦听器进行日志记录?

  1. 我需要创建我自己的TraceListenerData,像Tavares解决方案吗?

  2. 或者我需要理解和混乱unitydefaultbehavioreextension,由MacLeod详细?

  3. 现有代码是否错误?

  4. 或者解决方案在其他地方?

谢谢你的专业知识。

entlib CustomTraceListener unresolved

去获取Entlib 5可扩展性实验室。有一整章是关于编写自定义跟踪侦听器的。你会学到底层的技术,为什么事物是这样工作的,它们是如何组合在一起的,然后你就可以回答你自己的问题了。

至于上面为什么不起作用:当使用CustomXXXData配置类时,您的提供者需要一个接受NameValueCollection的构造函数。你没有,所以你的代码实际上是错误的。通过实验,你会学到更多。