Click here to Skip to main content
15,883,647 members
Articles / Programming Languages / C#
Tip/Trick

Getting notifications upon your application writing an error to Events Log

Rate me:
Please Sign up or sign in to vote.
4.92/5 (4 votes)
27 May 2012CPOL3 min read 14.4K   15  
You can use this trick for getting notifications when your app writes an error to event logs
This is an old version of the currently published tip/trick.

Introduction

A vast majority of applications choose to persist its internal state to Events log specially in case when the application encounters any errors. Depending upon error model implemented by the application, users may be notified of the any issues promptly but there could be situations when users may not get notified of these errors right away. In the later scenario, there is a potential that these unwarrnated errors could remain unnoticed and therefore miss to take any necessary corrective actions. This article will demonstrate how you can get a notification anytime an error is logged by your application in Event Logs.

Background

Our demo application is simple bare-minimum app shown in figure 1. When you click on the "Write To Event Log" button it throws an exception. These exception details are silently written in the Event Logs, however, user is not notified of the error. In this situation user may be completely unaware that an error has happened. Of course, the fundamental problem here is that we need to implement a better error handling mechanism where users are kept informed about error situations. However the trick I am going to show you could also be useful when application is still been developed and error situations like this could go un-noticed.

Image 1

Figure 1: Demo Application

As shown below, code for this application is also very simple. Again its a bare-minimum code only to demonstrate the concept.

C#
private void btnWrite_Click(object sender, EventArgs e)
{
    try
    {
        throw new Exception("Event Log Demo - Your Error Text");
    }
    catch(Exception ex)
    {
        WriteToEventLog(ex.Message + Environment.NewLine + ex.StackTrace);
    }
}

private void WriteToEventLog(string Message)
{
    string Source = "EventLogsDemo";

    if (!EventLog.SourceExists(Source))
        EventLog.CreateEventSource(Source, "Application");

    EventLog.WriteEntry(Source, Message, EventLogEntryType.Error);
}

Pre-requisite for this workflow is that this event has been logged at least once in the Event log. As shown in the figure 2 below, this event is already logged once in the Event Log.

Image 2

Figure 2: Error shown in the Event Viewer

At this point, if you right click on the error you will get an option for attaching a task with this event.

Image 3


Figure 3: Menu option for attaching task

When you click on this menu option, a wizard will start. At first step of wizard, you can define the name and description of this task.

Image 4

Figure 4: First step of wizard

Next step will just shows you the specific event that is logged.

Image 5

Figure 5: Event Log Source

At this point, you can choose specific action that you would like to happen when this error is logged in the Events Log. You can configure to run a program, fire an email or just display a message box. For this demo, I will use Display a message option. Please note that this option may not be the best for applications that run in background on production machine. For that type of scenario, firing off an email could be a better option to choose.

Image 6

Figure 6: Conguration of Action

In the next step, you just setup the title and message that will be displayed to user.

Image 7

Figure 7: Display Message Configuration

At this point you are pretty much all set. You will get a summary of all steps you configured in this wizard.

Image 8

Figure 8: Configuration Summary
Now if you run the application and click on Wirte To Event Log button, not only you will get an entry in Events Log but you will also get following prompt.

Image 9

Figure 9: Pop-up when error is logged in Events Log

What happened behind the scense is that a task has been added in your Task Scheduler. You can view this task under "Event Viwer Tasks".

Image 10

Figure 10: Error shown in the Event Viewer


I hope you will find this tip useful.

License

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


Written By
Architect
Canada Canada
Kamran Bilgrami is a seasoned software developer with background in designing mission critical applications for carrier grade telecom networks. More recently he is involved in design & development of real-time biometric based security solutions. His areas of interest include .NET, software security, mathematical modeling and patterns.

He blogs regularly at http://WindowsDebugging.Wordpress.com

Comments and Discussions

Discussions on this specific version of this article. Add your comments on how to improve this article here. These comments will not be visible on the final published version of this article.