2

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>
Community
  • 1
  • 1
IEnumerable
  • 3,610
  • 14
  • 49
  • 78
  • try initializeData="c:\trace.log" instead – Jordan Apr 25 '14 at 00:26
  • Thanks, Ive got an update, I got the error to go away but no loggin data is being reported – IEnumerable Apr 25 '14 at 00:28
  • for your config it looks like you need it to just be "trace.log" actually, but I think maybe it's not writing because you have "C:\\Test\\trace.log" for your Write method and "C:\\trace.log" for your WriteLine method - they may need to be the same – Jordan Apr 25 '14 at 00:33
  • Thanks martin, I have just fixed this now Im getting a IO error. An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll Additional information: The process cannot access the file 'C:\trace.log' because it is being used by another process. – IEnumerable Apr 25 '14 at 00:35
  • Also I believe you can just get rid of ", Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" following the YAGNI principle – Jordan Apr 25 '14 at 00:36
  • maybe try restarting your IDE or if a trace.log file already exists maybe try deleting it (or maybe the issue is that you have it open in something else that has a lock on it)? – Jordan Apr 25 '14 at 00:38
  • @Martin - Yeah. I should be able to figure this out. BTW, I removed the "Version=1.0..." Im curious as to what is the YAGNI principle, do you have a link ? – IEnumerable Apr 25 '14 at 00:39
  • "You aren't gonna need it" – Jordan Apr 25 '14 at 00:40
  • Make sure that in your project properties, under "Build", that you've enabled "Define TRACE constant". You won't see any output without that. – Brent Arias Aug 05 '18 at 21:37

0 Answers0