【问题标题】:Unable to connect custom tracelistener class via app config - ConfigurationErrorsException无法通过应用配置连接自定义跟踪侦听器类 - ConfigurationErrorsException
【发布时间】:2011-04-02 05:59:52
【问题描述】:

UPDATE - 现在不用回答了,我已经在下面解决了。

您好,我正在尝试在 .NET 中实现自定义跟踪侦听器,但是通过配置文件添加跟踪侦听器时遇到问题。

我发现了一篇关于堆栈溢出的类似帖子,但似乎没有帮助 (How to define custom TraceListener in app.config)。

异常信息是:

ConfigurationErrorsException - “无法创建 ApplicationFramework.TraceListeners.TextLogTraceListener、ApplicationFramework.TraceListeners、Version=1.0.0.0、Culture=neutral、PublicKeyToken=null。”

正如您在下面的代码中看到的,我什至在尝试不使用之后使用了 AssemblyQualified 名称。

config 和 dll 存在于引用监听器的应用程序中。

谁能发现我在这里做错了什么?

C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ApplicationFramework.TraceListeners
{
    public class TextLogTraceListener : System.Diagnostics.TextWriterTraceListener
    {
        public override void Write( string message )
        {
            using (FileStream fs = new FileStream( "C:\\Test\\trace.log", FileMode.Append ))
            {
                StreamWriter sw = new StreamWriter( fs );

                sw.Write( message );
            }
        }

        public override void WriteLine( string message )
        {
            using (FileStream fs = new FileStream( "C:\\Test\\trace.log", FileMode.Append ))
            {
                StreamWriter sw = new StreamWriter( fs );

                sw.Write( message );
            }
        }
    }
}

配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.diagnostics>

      <trace autoflush="true" indentsize="4">
        <listeners>
          <add name="TextListener"
              type="ApplicationFramework.TraceListeners.TextLogTraceListener, ApplicationFramework.TraceListeners, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
              initializeData="trace.log" />
          <remove name="Default" />
        </listeners>
      </trace>

  </system.diagnostics>

</configuration>

引用应用程序中的简单跟踪调用:

Trace.WriteLine( "Test" );

【问题讨论】:

    标签: c# .net


    【解决方案1】:

    我认为,如果您从 Web 配置中删除 InitializeData 并派生不带任何参数的构造函数就可以了,您似乎不需要使用的构造函数,因为您手动定义了 FileStream。

    【讨论】:

      【解决方案2】:

      不用担心,我现在已经解决了这个问题。

      我需要重写其中一个构造函数重载:

      public TextLogTraceListener(string name) : base(name) {

      }

      【讨论】:

      • +1 谢谢,从无用的错误消息中看不出这是你需要做的。
      猜你喜欢
      • 2016-05-12
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      • 2010-10-06
      • 1970-01-01
      • 2016-04-09
      • 2017-09-24
      • 2015-09-26
      相关资源
      最近更新 更多