Add a EventLogTraceListener to the listener collection and writing error messages to the Application log : Log : Development Class C# Source Code


Custom Search

C# Source Code » Development Class » Log »

 

Add a EventLogTraceListener to the listener collection and writing error messages to the Application log








    

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

// LogTrace.cs -- Demonstrates adding a EventLogTraceListener to the listener
//                collection and writing error messages to the Application log.
//
//                Compile this program with the following command line:
//                    C:>csc /debug:full /d:TRACE LogTrace.cs
using System;
using System.Diagnostics;

namespace nsEventLogs
{
    public class LogTrace
    {
        static public void Main ()
        {
// Create the EventLog object
            EventLog EvLog = new EventLog ();
// Register the source if it has not already been registered
            if (!EventLog.SourceExists ("AppLog.exe"))
            {
                Console.WriteLine ("Creating event log source");
                EventLog.CreateEventSource ("AppLog.exe", "Application");
            }
            EvLog.Source = "AppLog.exe";
            Trace.Listeners.Add (new EventLogTraceListener (EvLog));
            Trace.Listeners.Add (new TextWriterTraceListener (Console.Out));
            Trace.WriteLine ("Debugging to the event log");
// Set the source for the messages to be display in the Event Viewer
            EvLog.Source = "AppLog.exe";
        }
    }
}


           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Development Class
» Log