Logging Exceptions in Window’s Event Log

When we develop an application we do the exception logging using various ways. For example, logging exceptions in database, writing in text files, sending emails and etc.

In this article we will see how we can log the exceptions in window’s event logs and how to read the same.

To log exception in windows event log, you first need to import the System.Diagnostics name space to get the access of classes required to log the exceptions.

Below is the simple code which is logging the “divide by zero” exception.

            decimal a, b, result;
            a = "10.0";
            b = "0.0";
            result = a / b;
        catch (Exception err)
            // Write the information to the event log.
            // Register the event source if needed.
            if (!EventLog.SourceExists("DivideByZeroApp"))
                // This registers the event source and creates the custom log,
                // if needed.
                EventLog.CreateEventSource("DivideByZeroApp", "ProseTech");

            // Open the log. If the log doesn't exist,
            // it will be created automatically.
            EventLog log = new EventLog("ProseTech");
            log.Source = "DivideByZeroApp";
            log.WriteEntry(err.Message, EventLogEntryType.Error);


When you open the Windows event log you will get the exception source as “DivideByZeroApp” and on double click of that exception actual exception logged using Exception.Message property.

To read the exception from windows event log, below is the code.

 // For maximum performance, join all the event
 // information into one large string using the
 // StringBuilder.
 System.Text.StringBuilder sb = new System.Text.StringBuilder();
 EventLog log = new EventLog("ProseTech");
 foreach (EventLogEntry entry in log.Entries)
 // Write the event entries to the StringBuilder.
 if (entry.Source == "DivideByZeroApp")
   sb.Append("Entry Type:");
   sb.Append("Time Generated:");

DLL Hell

DLL hell is nothing but let say three applications are using the same dll for e.x. Spell Checker, then there is absolutly no problem in sharing the dll among these three, but bcz of some reason one application modified the dll then it will be of no use for others.

Another example: msword is using any dll and game installed on the same machine also using that and if we uninstall it then possibility of that dll corruption by game software is more.

.Net overcome this problem by maintaining versioning of dll(Assebmly : self discribing).There is no need to register it.

Terminology & TLA’s (Three Latter Acronyms)

CLR : Common Language Runtime.
Managed Code: COde that is managed by the CLR.
Unmanaged COde: Non-.Net Code.
IL; MSIL; PCode: Microsoft Intermediate Language Code.
ASP: Active Server Pages.
Application Service Provider.
GC: Garbage Collection
Build/Compile: Creates an assembly containing MSIL.
Protected Code: Code with error handling.
XML: eXtensible Markup Language.
Validated XML: XML that conforms to a DTD.
DTD: A schema for an XML document.
DataSet: .NET cousin to the ADO recordset.
IDE: Integrated Development Environment(VS.NET).
Code Behind: Separate location for event code for a web page.