Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to use the event log

0.00/5 (No votes)
26 Jul 2011 1  
How to configure your app to send messages to the event log.

Event log is a central repository for systems information, warning, and errors. Since it is used by most standard applications, I would also recomend it. In this tip/trick, I demonstrate how to configure your app to send messages to it. Read the comments to identify your options.


Add the following on your web.config:


XML
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
      <system.diagnostics>
    <trace autoflush="true">
      <listeners>
        <remove name="Default" />
        <clear />
        <add name="EventLogListener" type="System.Diagnostics.EventLogTraceListener"
                            initializeData="TITLE Displayed on source of event log" />
      </listeners>
    </trace>
    <switches>
      <add name="General" value="2" />
      <!--  Off Error Warning Info Verbose. Should be set to Error for production!!
      0 (off), 1 (error), 2 (warning), 3 (info), OR 4 (verbose)
      -->
    </switches>
  </system.diagnostics>
</configuration>

Example of usages:


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Configuration;
using Emailing;

namespace ConsoleVarious
{
    class Program
    {
        static void Main(string[] args)
        {
            //Define this in the web config
            TraceSwitch generalSwitch = new TraceSwitch("General", 
                                            "Entire Application");
            string odrReportID = "RC";
            string odrReportStatusID = "LC";
            string msgText = "AMC";
            // Write INFO type message, if switch is set to Verbose, type 4
            Trace.WriteIf(generalSwitch.TraceVerbose,
                        string.Format("UpdateODRContactReport method called with {0} {1} {2}.",
                        odrReportID, odrReportStatusID, msgText));
            // Write INFO type message, if switch is set to Verbose or Warning 4 0r 2
            Trace.WriteIf(generalSwitch.TraceWarning,
                        string.Format("Update method called {0}.",
                        "UpdateItNowMothed"));
            try
            {
                int b = 5;
                int c = 0;
                b = b / c;
            }
            catch (Exception ex)
            {
                // Write ERROR type message, if switch is set to Verbose, Warning, info or Error
                // 0 (off), 1 (error), 2 (warning), 3 (info), OR 4 (verbose)
                //If General switch in WEB CONFIG = 0 then it will not get into the if below
                if (generalSwitch.TraceError)
                {
                    //Trace type, inthis case error will define how it will appear in the event log
                    Trace.TraceError("Error Details: {0} Stack: {1}", 
                                     ex.Message, ex.StackTrace);
                    //Use your imagination to switch it on and off properly.
                    //You can really imagine and apply.
                }
            }
        }
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here