entlib CustomTraceListener尚未解决 [英] entlib CustomTraceListener unresolved

查看:102
本文介绍了entlib CustomTraceListener尚未解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是Enterprise Library的新功能(5,带有可选更新),并且正在尝试将我自己的CustomTraceListener与Logging Block一起使用.

im new to Enterprise Library (5, with the optional update), and am trying to use my own CustomTraceListener with the Logging Block.

使用提供的平面文件侦听器记录日志,但是我似乎无法将其扩展到我的自定义类.

Logging works with a supplied flat file listener, but i dont seem to be able to extend that to my custom class.

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

基于 bychkov的我的CustomListener :

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,我怀疑这是问题的一部分或全部?

First i tried to add the customtracelistener with the Configuration Editor, but the configuration editor did not list/detect any customtracelisteners, which i suspect is part, or whole of the problem?

因此,我根据

现在,当我调用Inf("logme")并添加上述app.config添加内容时,_container.AddNewExtension()会引发异常: 无法解析"Test.Catch.LoggerCustom,Test.Catch"类型.

Now when I call Inf("logme"), with the above app.config addition included, then an exception is thrown at _container.AddNewExtension(): The type 'Test.Catch.LoggerCustom, Test.Catch' cannot be resolved.

鉴于我不了解其基本机制,我该如何使用自定义侦听器进行日志记录?

Given I dont understand the underlying mechanisms, how could i get to logging with my custom listener?

  1. 我是否需要创建自己的TraceListenerData,例如解决方案吗?

还是我需要了解并弄混UnityDefaultBehaviorExtension,详情请参见

Or do i need to understand and mess with UnityDefaultBehaviorExtension, as detailed by MacLeod ?

现有代码是否错误?

还是解决方案在其他地方?

Or the solution lies elsewhere?

感谢您的专业知识.

推荐答案

去抢

Go grab the Entlib 5 extensibility labs. There's an entire chapter on writing custom trace listeners. You'll learn the underlying technologies, why things work the way they do, how it fits together, and then you'll be able to answer your own questions.

关于上述原因为何不起作用:使用CustomXXXData配置类时,提供程序需要一个带有NameValueCollection的构造函数.您没有一个,因此您的代码实际上是错误的.通过实验室工作,您将学到更多内容.

As for why the above doesn't work: when using CustomXXXData configuration classes, your provider needs a constructor that takes a NameValueCollection. You don't have one, so your code is in fact wrong. Work through the labs, you'll learn that and more.

这篇关于entlib CustomTraceListener尚未解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆