tags:

views:

53

answers:

1

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" );
A: 

No worries, I've solved the issue now.

I needed to override one of the constructor overloads:

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

}

BombDefused