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

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)

  • 如果你到了这里仍然没有它工作(因为我没有),就像@user3141326 建议的那样,为`public CustomTraceListener(string name) : base (name) { }` 提供一个构造函数覆盖。其他的可以省略。 (4认同)
  • 这给出了不同的消息(相同的异常类型)`无法创建MyApp.Utils.FormattedTextWriterTraceListener,MyApp。 (2认同)
  • 现在他找到了类型但无法创建实例,可能在构造函数或其他调用的方法中存在异常,请尝试在构造函数中设置断点并进行调试 (2认同)

小智 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)