Click here to Skip to main content
Click here to Skip to main content

Tagged as

How to use the event log

, 26 Jul 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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 version="1.0" encoding="utf-8" ?>
    <trace autoflush="true">
        <remove name="Default" />
        <clear />
        <add name="EventLogListener" type="System.Diagnostics.EventLogTraceListener"
                            initializeData="TITLE Displayed on source of event log" />
      <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)

Example of usages:

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
                        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
                        string.Format("Update method called {0}.",
                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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

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 --
| Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 26 Jul 2011
Article Copyright 2011 by Marcio_Coelho
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid