In my Application (C# Winforms) Im trying to implement a custom trace listener like This one here.
But Im getting a configuration error.
Config File
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="TextListener"
type="MyApp.TextLogTraceListener, MyApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
initializeData="c:\\trace.log" />
<remove name="Default" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
Custom Listener
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace MyApp
{
public class TextLogTraceListener : System.Diagnostics.TextWriterTraceListener
{
public TextLogTraceListener()
{
}
public TextLogTraceListener(string name)
: base(name)
{
}
public override void Write(string message)
{
using (FileStream fs = new FileStream("C:\\trace.log", FileMode.Append))
{
StreamWriter sw = new StreamWriter(fs);
sw.Write(message);
}
}
public override void WriteLine(string message)
{
using (FileStream fs = new FileStream("C:\\trace.log", FileMode.Append))
{
StreamWriter sw = new StreamWriter(fs);
sw.Write(message);
}
}
}
}
Error Im Getting
An unhandled exception of type 'System.Configuration.ConfigurationErrorsException' occurred in System.dll
Additional information: Couldn't find type for class MyApp.TraceListeners.TextLogTraceListener, MyApp.TraceListeners, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
My Main objective is to catch all Debug/Trace information into my own custom text file.
UPDATE I fixed the namespace which seemed to remove the error and as Martin suggested I changed the initizeData. Now I have removed the error thrown in VS I am still not getting data reported to the text file.
Im writing trace logs like this.
Trace.WriteLine("Application Starting");
Config File
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="TextListener"
type="MyApp.TextLogTraceListener, MyApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
initializeData="c:\trace.log" />
<remove name="Default" />
</listeners>
</trace>
</system.diagnostics>
</configuration>