Team LiB
Previous Section Next Section

Creating and Writing to an Event Log

You can also use the following code to write directly to the event log without using Trace methods.

EventLog Log = new EventLog();
if (!EventLog.SourceExists("Remoting"))
        EventLog.CreateEventSource("Remoting", "Application");
Log.Source = "Remoting";
Log.WriteEntry(System.Security.Principal.WindowsIdentity.GetCurrent().Name);

The following example shows how to create and write to an event log.

Code Example: Creating and Writing to an Event Log
Start example
using System;
using System.Diagnostics;

namespace EventLogging
{
          class Class1
          {


                [STAThread]
                static void Main(string[] args)
                {
                      //Creates a new event log in Event Viewer
                       EventLog E = new EventLog("ApplicationLog");
                       E.Source = "MyApplication";
                      //Writes an entry in the log
                       E.WriteEntry("My Logged Event");
                      E.WriteEntry("My2ndEvent",EventLogEntryType.Error, 5555, 2);
                }
          }
}
End example

Team LiB
Previous Section Next Section