Writing Log to Event Viewer in C#


While developing any application we need to have a logging mechanism to track exceptions occurred in the application and also to track specific user actions. In this article we will discuss about how to implement the logging mechanism to track exceptions and other events in windows event viewer in C# application.

Open Microsoft Visual Studio 2015 => Create C# Console Application and name it as CSharpEventViewer. We have to use EventLog class available System.Diagnostics namespace to write log into event viewer. Application should run with Administrator privileges to write the log to Event Viewer.

Below code writes the message to Application log in Event Viewer.

            //Example to show how to write error message in the Event Viewer

            string logSource = "CSharpEventViewer";

            string logMsg = "This is example message to write log to Event Viewer";

            string logName = "Application";

            int iEventId = 102;

 

            if (!EventLog.SourceExists(logSource))

                EventLog.CreateEventSource(logSource, logName);

 

            EventLog.WriteEntry(logSource, logMsg, EventLogEntryType.Error, iEventId);

 

As shown above we have to provide source name for which message logged, logging message, the log name of the Event Viewer, message type (Error or warning or Information) and finally event id. The above code writes the message to event viewer as shown below.

In the same way we can write Warning and Information messages also in Event Viewer as shown below.

using System.Diagnostics;

 

namespace CSharpEventViewer

{

    class Program

    {

        static void Main(string[] args)

        {

            ////Example to show how to write error message in the Event Viewer

            //string eventSourec = "CSharpEventViewer";

            //string logMsg = "This is example message to write log to Event Viewer";

            //string logName = "Application";

            //int iEventId = 102;

 

            //if (!EventLog.SourceExists(eventSourec))

            //    EventLog.CreateEventSource(eventSourec, logName);

 

            //EventLog.WriteEntry(eventSourec, logMsg, EventLogEntryType.Error, iEventId);

 

            //=============================================================================================

 

            ////Example to show how to write warning message in the Event Viewer

            //string eventSourec = "CSharpEventViewer";

            //string logMsg = "This is example message to write warning message to Event Viewer";

            //string logName = "Application";

            //int iEventId = 103;

 

            //if (!EventLog.SourceExists(eventSourec))

            //    EventLog.CreateEventSource(eventSourec, logName);

 

            //EventLog.WriteEntry(eventSourec, logMsg, EventLogEntryType.Warning, iEventId);

 

            //=============================================================================================

 

            //Example to show how to write Information in the Event Viewer

            string eventSourec = "CSharpEventViewer";

            string logMsg = "This is example message to write Information to Event Viewer";

            string logName = "Application";

            int iEventId = 103;

 

            if (!EventLog.SourceExists(eventSourec))

                EventLog.CreateEventSource(eventSourec, logName);

 

            EventLog.WriteEntry(eventSourec, logMsg, EventLogEntryType.Information, iEventId);

        }

    }

}

 

Warning and Information messages displays like below in Event Viewer.