加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 04:25:24 所属栏目:百科 来源:网络整理
导读:更新 – 现在无需回答,我已在下面解决了. 嗨,我正在尝试在.NET中实现自定义跟踪侦听器,但是在通过配置文件添加跟踪侦听器时遇到问题. 我在堆栈溢出时发现了一个类似的帖子,但它似乎没有帮助(How to define custom TraceListener in app.config). 异常消息是
更新 – 现在无需回答,我已在下面解决了.

嗨,我正在尝试在.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:Testtrace.log",FileMode.Append ))
            {
                StreamWriter sw = new StreamWriter( fs );

                sw.Write( message );
            }
        }

        public override void WriteLine( string message )
        {
            using (FileStream fs = new FileStream( "C:Testtrace.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)
{

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读