【问题标题】:How to define custom TraceListener in app.config如何在 app.config 中定义自定义 TraceListener
【发布时间】:2010-11-13 16:16:16
【问题描述】:

我已经实现了一个自定义跟踪侦听器(源自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 的类型。

有谁知道如何在配置中设置此自定义侦听器以及是否可行?

【问题讨论】:

    标签: .net configuration exception-handling trace trace-listener


    【解决方案1】:

    也尝试指定一个程序集,如下所示:

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

    【讨论】:

    • 程序集名称是MyApp,我将其添加到类型参数
    • 这给出了不同的消息(相同的异常类型)`Could not create MyApp.Utils.FormattedTextWriterTraceListener, MyApp.
    • 现在他找到了类型但无法创建实例,可能是构造函数或其他调用的方法有异常,尝试在costructor设置断点并调试
    • 对我来说,是我忘记覆盖一些基本构造函数。请记住,在智能感知中显示了 4 个基本构造函数 - 当您按下很容易被忽略的向下箭头时,还有两个。
    • 如果你到了这里但仍然没有工作(因为我没有),就像@user3141326 建议的那样,为public CustomTraceListener(string name) : base (name) { } 提供一个构造函数覆盖。其他的可以省略。
    【解决方案2】:

    我最近一直在努力解决这个问题,以防万一它对任何人有所帮助......

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

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

    在我的例子中,myClassType.AssemblyQualifiedName 包含我在 app.config 文件中的 type 属性中需要的字符串。

    例如:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-21
      • 1970-01-01
      • 1970-01-01
      • 2015-08-11
      • 1970-01-01
      • 2010-11-12
      • 2012-10-28
      相关资源
      最近更新 更多