Question

I am using TraceListener for writing the execution details to a log file. Below is the configuration in my app.config file:

<system.diagnostics>
    <trace autoflush="true" indentsize="4">
      <listeners>
        <add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="Runner.log" />
        <remove name="Default" />
      </listeners>
    </trace>
  </system.diagnostics>

However I can see for every execution the logs are being appended to the file Runner.log. Instead I want to create different log files for every execution. Is there any setting I need to add in app.config file ? Or can this be done programmatically ? Appreciate your help.

Was it helpful?

Solution

I have implemented below class and it is working fine for me. Also, now I don't need to specify anything in the app.config file

  class TextLogTraceListener : TextWriterTraceListener
    {
        private string logFileLocation = string.Empty;

        private StreamWriter traceWriter;

        public TextLogTraceListener(string filePath)
        {
            logFileLocation = filePath;
            traceWriter = new StreamWriter(filePath, true);
            traceWriter.AutoFlush = true;
        }

        public override void Write(string message)
        {
            traceWriter.Write(message);
        }

        public override void WriteLine(string message)
        {
            traceWriter.WriteLine(message);
        }

        public override void Close()
        {
            traceWriter.Close();
        }
    }

And using this class like below:

public static class Logger
    {
        static TextLogTraceListener textLogTraceListener = new TextLogTraceListener("Trace.log");

        public static void CloseWriter()
        {
            textLogTraceListener.Close();
        }

        public static void Error(string message, string module)
        {
            WriteEntry(message, "ERROR", module);
        }

        public static void Error(Exception ex, string module)
        {
            WriteEntry(ex.Message + Environment.NewLine + ex.StackTrace, "ERROR", module);
        }

        public static void Warning(string message, string module)
        {
            WriteEntry(message, "WARNING", module);
        }

        public static void Info(string message, string module)
        {
            WriteEntry(message, "INFO", module);
        }

        private static void WriteEntry(string message, string type, string module)
        {
            textLogTraceListener.WriteLine(string.Format("[{0}] [{1}] [{2}] {3}",
                                  DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                  type,
                                  module,
                                  message));
        }
    }

OTHER TIPS

Best way would be to write your own implementation of the TextWriterTraceListener class and register this in your app.config instead.

Here´s an example implementation:

http://www.geekzilla.co.uk/View2C5161FE-783B-4AB7-90EF-C249CB291746.htm

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top