无法通过应用程序配置自定义连接TraceListener类 - ConfigurationErrorsException [英] Unable to connect custom tracelistener class via app config - ConfigurationErrorsException
问题描述
更新 - 没有必要现在回答这个问题,我已经解决了以下
你好,我想.NET实现一个自定义跟踪监听器,但有一个问题,通过配置文件中添加跟踪侦听器。
我发现堆栈溢出类似的职位,但它似乎并没有帮助(http://stackoverflow.com/questions/1176582/how-to-define-custom-tracelistener-in-app-config)。
异常消息是:
ConfigurationErrorsException - 无法创建ApplicationFramework.TraceListeners.TextLogTraceListener,ApplicationFramework.TraceListeners,版本= 1.0.0.0,文化=中性公钥=空。
正如你可以在我的code见下文,我想没有之后甚至使用了AssemblyQualified名称。
的配置和DLL中存在引用监听器的应用程序。
任何人能发现我可能是错在这里做什么?
C#code:
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.IO;
命名空间ApplicationFramework.TraceListeners
{
公共类TextLogTraceListener:System.Diagnostics.TextWriterTraceListener
{
公共覆盖无效写入(字符串消息)
{
使用(的FileStream FS =新的FileStream(C:\\测试\\ trace.log的,FileMode.Append))
{
StreamWriter的SW =新的StreamWriter(FS);
sw.Write(消息);
}
}
公众覆盖无效的WriteLine(字符串消息)
{
使用(的FileStream FS =新的FileStream(C:\\测试\\ trace.log的,FileMode.Append))
{
StreamWriter的SW =新的StreamWriter(FS);
sw.Write(消息);
}
}
}
}
配置:
< XML版本=1.0编码=UTF-8&GT?;
<结构>
< System.Diagnostics程序>
<跟踪自动冲洗=真indentsize =4>
<听众>
<添加名称=的TextListener
TYPE =ApplicationFramework.TraceListeners.TextLogTraceListener,ApplicationFramework.TraceListeners,版本= 1.0.0.0,文化=中性公钥=空
initializeData =trace.log的/>
<删除名称=默认/>
< /听众>
< /跟踪>
< /system.diagnostics>
< /结构>
在引用的应用程序简单的跟踪电话:
Trace.WriteLine(测试);
不用担心,我现在已经解决了这个问题。
我需要重写构造函数重载之一:
公共TextLogTraceListener(字符串名称):基地(名) {
}
UPDATE - No need to answer this now, I have solved below.
Hi, I'm trying to implement a custom trace listener in .NET, but having a problem adding the trace listener via the config file.
I found a similar post on stack overflow but it doesn't seem to help (http://stackoverflow.com/questions/1176582/how-to-define-custom-tracelistener-in-app-config).
The exception message is:
ConfigurationErrorsException - "Could not create ApplicationFramework.TraceListeners.TextLogTraceListener, ApplicationFramework.TraceListeners, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null."
As you can see in my code below, I have even used the AssemblyQualified name after trying without.
The config and dll exist in the application that references the listener.
Can anyone spot what I might be doing wrong here?
C# Code:
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 );
}
}
}
}
Config:
<?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>
Simple trace call in referencing application:
Trace.WriteLine( "Test" );
No worries, I've solved the issue now.
I needed to override one of the constructor overloads:
public TextLogTraceListener( string name ) : base( name ) {
}
这篇关于无法通过应用程序配置自定义连接TraceListener类 - ConfigurationErrorsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!