Failed to connect custom tracelistener class through app config - ConfigurationErrorsException

UPDATE - no need to answer this now, I decided below.

Hi, I am trying to implement my own trace listener in .NET, but with the problem of adding a trace listener through the configuration file.

I found a similar entry in the stack overflow, but it doesn't seem to help ( How to define a custom TraceListener in app.config ).

Exception Message:

ErrExException - "Failed to create ApplicationFramework.TraceListeners.TextLogTraceListener, ApplicationFramework.TraceListeners, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null."

As you can see in my code below, I even used the name AssemblyQualified after trying without it.

Configuration and dll exist in the application that references the listener.

Can anyone determine what I can do 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> 

A simple trace call in the application for links:

 Trace.WriteLine( "Test" ); 
+6
c #
source share
2 answers

Don’t worry, I solved the problem now.

I needed to override one of the constructor overloads:

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

}

+12
source share

I think that if you removed InitializeData from the web configuration and displayed the constructor without any parameters, it would be all right, you do not need the constructor that you use because you define the FileStream manually.

0
source share

All Articles