更新 – 现在无需回答,我已在下面解决了.
嗨,我正在尝试在.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" );