微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 无法通过app config连接自定义tracelistener类 – ConfigurationErrorsException

更新 – 现在无需回答,我已在下面解决了.

嗨,我正在尝试在.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,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null"
              initializeData="trace.log" />
          <remove name="Default" />
        </listeners>
      </trace>

  </system.diagnostics>

</configuration>

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

Trace.WriteLine( "Test" );

解决方法

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

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

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

}

原文地址:https://www.jb51.cc/csharp/91608.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐