RaY*_*ell 63 .net configuration trace exception-handling trace-listener
我已经实现了一个自定义跟踪侦听器(派生自TextWriteTraceListener
),现在我想将我的应用程序设置为使用它而不是标准TextWriteTraceListener
.
首先,我添加了默认值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>
Run Code Online (Sandbox Code Playgroud)
现在我的跟踪侦听器在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>
Run Code Online (Sandbox Code Playgroud)
但是现在,当我尝试记录一些我正在收到ConfigurationErrorsException
消息时:
找不到类MyApp.Utils.FormattedTextWriterTraceListener的类型.
有谁知道如何在配置中设置这个自定义监听器,如果它甚至可能?
Ars*_*yan 83
尝试指定程序集,如下所示:
<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>
Run Code Online (Sandbox Code Playgroud)
小智 5
我最近一直在为此努力,以防万一它可以帮助任何人...
我知道我的类型存在,所以写了以下内容:
Assembly assembly = System.Reflection.Assembly.GetAssembly(typeof("yourclassname"));
Type myClassType = assembly.GetType("yournamespace.yourclassname");
Run Code Online (Sandbox Code Playgroud)
就我而言,myClassType.AssemblyQualifiedName在type属性中包含我在app.config文件中所需的字符串。
例如:
<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>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
36083 次 |
最近记录: |