0

I want to add event log tracing to my winforms application but in don't understand to much about this.

What i have done is this steps: In my app.config:

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

A basic logger class like this:

using System;
using System.Diagnostics;

namespace LogEventSample
{
    public static class Logger
    {
        public static void Error(string message, string module)
        {
            WriteEntry(message, "error", module);
        }

        public static void Error(Exception ex, string module)
        {
            WriteEntry(ex.Message, "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)
        {
            Trace.WriteLine(string.Format("{0},{1},{2},{3}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), type, module, message)); 
        }
    }
}   

And then, when I need a line like this:

Logger.Info("working...", "MyApp");

My doubts comes with the application lifetime. This will impact in my application performance? should i make this in a different way?

Also, how can i store more information when i'm loggin this? i do not talk about storing some exception string but to save more detailed data to show in the event viewer.

Vinod Srivastav
  • 3,644
  • 1
  • 27
  • 40
Phoenix_uy
  • 3,173
  • 9
  • 53
  • 100

1 Answers1

1

The way you are doing it is the standard way to trace application feedback in .NET, so it should not be "wrong".

Also your application performance isn't affected, because Trace.WriteLine calls are only resolved within debug builds. If you take a look at the definition, you will notice, that it is annotated with the Conditional("TRACE") attribute. So if you change your build profile to "Release", the property "Define TRACE-constant" of your project get's disabled (by default) and you will not see any trace output (not even into your log, so check if this is your desired behaviour!).

Also note that logging should not be your first optimization priority. Algorithms with better runtime behaviour can compensate most overhead and should be the way to start.

If you want more control over how data get's logged, I bet you are best with a good logging framework like Semantic Logging or Log4Net.

Carsten
  • 11,287
  • 7
  • 39
  • 62
  • I want to log events in the release profile, in my final to client application so the client tracks some actions in the application. `Trace` is not a good approach to this? Yo say that i will need to use Log4Net? – Phoenix_uy Aug 26 '15 at 20:13
  • If you want to include `Trace` output, just check the *Define TRACE-Constant* of your project after you changed your build profile to *Release*. The decision for a certain logging framework depends on many factors and is not part of your question. For more information on it check [this question](http://stackoverflow.com/questions/576185/logging-best-practices) – Carsten Aug 26 '15 at 20:15
  • I think i will go for that option. Because i don't want just to store errors but i want to log some actions that my client need to log. Also, it's possible to log more information besides a simple (or complex) string? – Phoenix_uy Aug 26 '15 at 20:17
  • Not using `Trace`, which is basically a tool to print simple program feedback (for example into the *Output* window of Visual Studio). – Carsten Aug 26 '15 at 20:18
  • But what about all the information that i see in a particular event in the Event Viewer? like `Event ID`, `Level`, `User`, `Task Category`, `Keywords` and so? do you mean that i should use Log4Net maybe? – Phoenix_uy Aug 26 '15 at 20:20
  • 1
    If you want to use those fields, either directly use [`System.Diagnostics.EventLog`](https://msdn.microsoft.com/de-de/library/system.diagnostics.eventlog%28v=vs.110%29.aspx) (which is there to directly write into the Windows Event Log) or go with some framework. Basically all of them that log into the Windows Event Log are abstracting away the `EventLog`-code to the and provide features like multi-target logging. `EventLogTraceListener` also only writes `Trace` into the `EventLog`. However, I don't know if Log4Net supports all of the fields (like the event log icons or id). – Carsten Aug 26 '15 at 20:28