Click here to Skip to main content
15,888,351 members
Articles / Web Development / ASP.NET
Tip/Trick

How to use the event log

Rate me:
Please Sign up or sign in to vote.
4.00/5 (2 votes)
26 Jul 2011CPOL 23.2K   12  
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Financial Institution
United States United States
Technology Developer with 15 years of experience in the IT industry, mostly spent with financial applications and financial business models. Played various roles such as leadership, management, system architect, database modeling and development, middle tier code and development, GUI, Java Script, C#, VB, SQL Server, Sybase Power Design, Red Gate, Scribe, Metastorm, Telerik, Infragistic, Aspose, Silverlight, Visual studio 2010 and others.

Comments and Discussions

 
-- There are no messages in this forum --