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.
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.
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.
private void btnWrite_Click(object sender, EventArgs e)
throw new Exception("Event Log Demo - Your Error Text");
WriteToEventLog(ex.Message + Environment.NewLine + ex.StackTrace);
private void WriteToEventLog(string Message)
string Source = "EventLogsDemo";
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.
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.
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.
Figure 4: First step of wizard
The next step will just shows you the specific event that is logged.
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.
Figure 6: Configuration of Action
In the next step, you just setup the title and message that will be displayed to user.
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.
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.
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".
Figure 10: Error shown in the Event Viewer
I hope you will find this tip useful.
- 27th May, 2012: First version added