如何在 app.config 中定义自定义 TraceListener [英] How to define custom TraceListener in app.config

查看:25
本文介绍了如何在 app.config 中定义自定义 TraceListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一个自定义跟踪侦听器(派生自 TextWriteTraceListener),现在我想将我的应用程序设置为使用它而不是标准的 TextWriteTraceListener.

首先我添加了默认的 TextWriteTraceListener 以确保它正常工作并且确实如此.这是我的 app.config:

<预><代码><配置><系统诊断><trace autoflush="true" indentsize="4"><听众><add name="TextListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="trace.log"/><删除名称=默认"/></听众></trace></system.diagnostics></配置>

现在我的跟踪侦听器在 MyApp.Utils 命名空间中定义,它被称为 FormattedTextWriterTraceListener.所以我将上面配置中的类型更改为 MyApp.Utils.FormattedTextWriterTraceListener 并且它目前看起来像这样:

<预><代码><配置><系统诊断><trace autoflush="true" indentsize="4"><听众><add name="MyTextListener" type="MyApp.Utils.FormattedTextWriterTraceListener" initializeData="trace.log"/><删除名称=默认"/></听众></trace></system.diagnostics></配置>

但是现在,当我尝试记录某些内容时,我收到了带有消息的 ConfigurationErrorsException:

<块引用>

找不到类 MyApp.Utils.FormattedTextWriterTraceListener 的类型.

有谁知道我如何在配置中设置这个自定义侦听器,甚至可能吗?

解决方案

也尝试指定程序集,如下所示:

<预><代码><配置><系统诊断><trace autoflush="true" indentsize="4"><听众><添加名称=文本监听器"type="MyApp.Utils.FormattedTextWriterTraceListener, MyApp"initializeData="trace.log"/><删除名称=默认"/></听众></trace></system.diagnostics></配置>

I have implemented a custom trace listener (derived from TextWriteTraceListener) and now I would like to set my application to use it instead of standard TextWriteTraceListener.

First I added default TextWriteTraceListener in order to make sure it works ok and it does. Here's my app.config:

<configuration>
    <system.diagnostics>
        <trace autoflush="true" indentsize="4">
            <listeners>
                <add name="TextListener"  type="System.Diagnostics.TextWriterTraceListener" initializeData="trace.log" />
            <remove name="Default" />
            </listeners>
        </trace>
    </system.diagnostics>
</configuration>

Now my trace listener is defined in MyApp.Utils namespace and it's called FormattedTextWriterTraceListener. So I changed the type in the config above to MyApp.Utils.FormattedTextWriterTraceListener and it currently looks like that:

<configuration>
    <system.diagnostics>
        <trace autoflush="true" indentsize="4">
            <listeners>
                <add name="MyTextListener" type="MyApp.Utils.FormattedTextWriterTraceListener" initializeData="trace.log" />
            <remove name="Default" />
            </listeners>
        </trace>
    </system.diagnostics>
</configuration>

However now when I try to log something I'm getting a ConfigurationErrorsException with the message:

Couldn't find type for class MyApp.Utils.FormattedTextWriterTraceListener.

Does anyone knows how can I set up this custom listener in config and if it's even possible?

解决方案

Try specifying an assembly too, like so:

<configuration>
    <system.diagnostics>
        <trace autoflush="true" indentsize="4">
            <listeners>
                <add name="TextListener" 
                    type="MyApp.Utils.FormattedTextWriterTraceListener, MyApp"
                    initializeData="trace.log" />
            <remove name="Default" />
            </listeners>
        </trace>
    </system.diagnostics>
</configuration>

这篇关于如何在 app.config 中定义自定义 TraceListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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