Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Getting Notifications Upon Your Application Writing an Error to Events Log

4.92/5 (4 votes)
12 Feb 2021CPOL3 min read 14.8K  
You can use this trick for getting notifications when your app writes an error to event logs

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 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 unwarranted errors could remain unnoticed and therefore miss taking 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 a 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 the application is still been developed and error situations like this could go un-noticed.

Image 1

Figure 1: Demo Application

As shown below, the code for this application is also very simple. Again, it's 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 the first step of wizard, you can define the name and description of this task.

Image 4

Figure 4: First step of wizard

The 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 the background on production machine. For that type of scenario, firing off an email could be a better option to choose.

Image 6

Figure 6: Configuration 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 Write To Event Log button, not only will you get an entry in Events Log, but you will also get the following prompt.

Image 9

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

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

Image 10

Figure 10: Error shown in the Event Viewer

I hope you will find this tip useful.

History

  • 27th May, 2012: First version added

License

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