.net 如何在 app.config 中定义自定义 TraceListener
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1176582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to define custom TraceListener in app.config
提问by RaYell
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),现在我想将我的应用程序设置为使用它而不是标准TextWriteTraceListener。
First I added default TextWriteTraceListenerin order to make sure it works ok and it does. Here's my app.config:
首先,我添加了默认值TextWriteTraceListener以确保它正常工作并且确实如此。这是我的 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.Utilsnamespace and it's called FormattedTextWriterTraceListener. So I changed the type in the config above to MyApp.Utils.FormattedTextWriterTraceListenerand it currently looks like that:
现在我的跟踪侦听器在MyApp.Utils命名空间中定义,它被称为FormattedTextWriterTraceListener. 所以我将上面配置中的类型更改为MyApp.Utils.FormattedTextWriterTraceListener,目前看起来像这样:
<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 ConfigurationErrorsExceptionwith the message:
但是,现在当我尝试记录一些东西时,我收到了一条ConfigurationErrorsException消息:
Couldn't find type for class MyApp.Utils.FormattedTextWriterTraceListener.
找不到类 MyApp.Utils.FormattedTextWriterTraceListener 的类型。
Does anyone knows how can I set up this custom listener in config and if it's even possible?
有谁知道如何在配置中设置这个自定义侦听器,甚至可能吗?
回答by Arsen Mkrtchyan
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>
回答by NobleGuy
I've been struggling with this recently and just in case it helps anyone...
我最近一直在努力解决这个问题,以防万一它可以帮助任何人......
I knew my type existed so I wrote the following:
我知道我的类型存在所以我写了以下内容:
Assembly assembly = System.Reflection.Assembly.GetAssembly(typeof("yourclassname"));
Type myClassType = assembly.GetType("yournamespace.yourclassname");
In my case, myClassType.AssemblyQualifiedName contained the string I needed in my app.config file in the type attribute.
就我而言,myClassType.AssemblyQualifiedName 包含我在 app.config 文件中的 type 属性中需要的字符串。
For example:
例如:
<system.diagnostics>
<sources>
<source name="System.ServiceModel" switchValue="Information,ActivityTracing" propagateActivity="true">
<listeners>
<add name="CircularTraceListener" />
</listeners>
</source>
</sources>
<sharedListeners>
<add name="CircularTraceListener" type="Microsoft.Samples.ServiceModel.CircularTraceListener, CircularTraceListener, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
initializeData="C:\MyWebService\APILog\CircularTracing-service.svclog" maxFileSizeKB="1000" />
</sharedListeners>
<trace autoflush="true" />
</system.diagnostics>


