如何在app.config中定义自定义TraceListener

66

我已经实现了一个自定义跟踪侦听器(派生自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>

现在我的跟踪监听器定义在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>

然而现在当我尝试输出日志时,会收到一个ConfigurationErrorsException异常,其中包含以下信息:

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

有人知道如何在配置中设置此自定义侦听器,是否可能?

2个回答

85

尝试同时指定一个程序集,像这样:

<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>

3
这个错误信息表达了一个不同的意思(相同的异常类型):“无法创建MyApp.Utils.FormattedTextWriterTraceListener,MyApp。” - RaYell
3
现在他找到了类型,但无法创建实例,可能构造函数或其他被调用的方法中出现了异常,请尝试在构造函数中设置断点并进行调试。 - Arsen Mkrtchyan
请确保您的项目(在定义配置的项目中)实际上有对MyApp项目的引用。 - BornToCode
1
对我来说,问题在于我忘记了重写一些基础构造函数。请注意,在智能感知中会显示4个基础构造函数 - 当您按下箭头时,还有两个可以轻易被忽略。 - user3141326
4
如果你已经到这里了,但仍然没有让它工作(就像我一样),可以像@user3141326建议的那样,为“public CustomTraceListener(string name) : base (name) { }”提供一个构造函数重载。其他的可以省略。 - J.D. Mallen
显示剩余5条评论

6

最近我一直在苦恼这个问题,如果有帮助到任何人的话...

我知道我的类型存在,所以我写了以下内容:

Assembly assembly = System.Reflection.Assembly.GetAssembly(typeof("yourclassname"));
Type myClassType = assembly.GetType("yournamespace.yourclassname");

在我的情况下,myClassType.AssemblyQualifiedName包含了我在app.config文件中需要的字符串,它位于type属性中。
例如: enter image description here
<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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接