如何界定app.config中自定义的TraceListener [英] How to define custom TraceListener in app.config
问题描述
我实现了一个自定义跟踪监听器(从 TextWriteTraceListener
导出),现在我想设置我的应用程序来使用它,而不是标准的 TextWriteTraceListener
。
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
.
首先,我增加了默认的 TextWriteTraceListener
,以确保它的工作原理确定,它的作用。这是我的app.config:
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>
现在我的跟踪监听器被定义在 MyApp.Utils
命名空间,这就是所谓的 FormattedTextWriterTraceListener
。所以我改变了上面的配置类型为 MyApp.Utils.FormattedTextWriterTraceListener
和它目前看起来像这样:
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>
但是现在,当我尝试登录的东西,我得到一个 ConfigurationErrorsException
与消息:
找不到类型类MyApp.Utils.FormattedTextWriterTraceListener。
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屋!