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.
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.
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.
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 first step of wizard, you can define the name and description of this task.
Figure 4: First step of wizard
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 background on production machine. For that type of scenario, firing off an email could be a better option to choose.
Figure 6: Conguration 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 Wirte To Event Log button, not only you will get an entry in Events Log but you will also get following prompt.
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".
Figure 10: Error shown in the Event Viewer
I hope you will find this tip useful.
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