Unable to connect custom tracelistener class via app config - ConfigurationErrorsException
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 (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>
<开发者_如何学C/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 ) {
}
I think if you removed the InitializeData from the web config and derive the constructor without any parameter you would have been fine, you don't seem to need the constructor you use because you define the FileStream manually.
精彩评论