Adding to Event Logs with VB.NET

Damn! What the hell happened to that application I was running? One minute I am viewing the pictures of my trip to the playboy mansion and another minute I am having to shut down the app. It is seriously not cool! Now only if there was a way I can record some of my applications activity for windows to see later. Perhaps describing why my application decided to take a dump on me would help the situation. Oh wait, there is! The Windows event logs! There we can record messages for users to view after the fact. I will show you how to make a standard VB.NET application access these logs, the types of logs there are and how to go about viewing them through XP. We cover the topic on this entry of the Programming Underground!

So first lets cover how we even get to the event logs so that we can see our applications magic in action. On Windows XP, you can go to Start >> Settings >> Control Panel >> Performance and Maintenance >> Administrative Tools >> Event Viewer. Here you should see several types of events that are being logged on your system. It is a good idea to visit this viewer once in awhile to make sure things are running as they should and no big error messages are being generated. The logs you should see is Application, Security, System and Internet Explorer.

Each one of these covers different types of events. One is for applications (where we will put our events), security is for logins, System is for windows system processes and then of course one for Internet Explorer (bleh!). It is important to remember these types because they correspond to the types of logs you will see in the “Log” property of our EventLog control in our project.

Since we are creating an application, it makes sense that we would put application messages in the applications log. So start a new project in VB.NET. In the control toolbox you will see a control “EventLog” which should be located under the Components sub menu of the toolbox. Dragging this control onto the form will put it in the projects tray (that location where other controls like datasets, timers, and imagelists go). Once we have that in our project, we can configure it by selecting it and going to the properties window. There will be a few fields here we will want to change. The first is the Log field, which we discussed above, is the type of logging we are going to be doing. We can set this to the Application log. The second field is the “Source” field which is the name that is going to be given to the source of the message in the logs. Here I put “My Application”. Any time the application logs an entry, it will say that it came from “My Application” as the source. Make sure to keep this short and descriptive so that when you see the error, you know what the source actually means. Lastly, give the control a good standard name. I will leave mine as EventLog1 just to make it generic.

Ok, so we are now all configured. How do we use it to log into the event logs? Simple! There is a method called “WriteEntry” which will take a string as our message along with 8 other overloads that you can play with. Some of them will take eventIDs, others will take event types etc. I encourage you to make sure that the message is very precise and includes any error numbers that may help users understand what is actually going on. You want them to know why your application had to close or why it was important that the message be logged. You don’t want to be logging EVERYTHING into the log, but key crucial messages may be helpful.

In the code below we use a simple button event to write our message into the log…

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		' Write two entries, one is a standard info entry, the other is a warning that our application is hostile!
		EventLog1.WriteEntry("Our application generated an entry in the application log!")
		EventLog1.WriteEntry("Warning: Our application is hostile!", System.Diagnostics.EventLogEntryType.Warning)
End Sub

So as you can see, writing to the log is not hard and we can easily customize the message to be an error, warning, or just informational message to name a few. We can include event IDs that may or may not correspond to Windows event IDs and can add whatever information we want to the message field. Perhaps we have an info dump or just to let the user know that we started a service on their computer.

After we run the code above, we can go look in the log viewer to see if we see our messages….

Windows Event Log

And there we go, we see our events recorded in the event logs for applications and one is our warning. That is all there is to it. Of course since this is a .NET application you could use both VC++ and C# to do something similar. You could even create this control on the fly and configure it through code. Ideally only one event log class would be needed and you could configure it to log all your activities to the different logs. Play around with it and see what you can do with it. Just don’t flood the user’s logs with useless information. Keep in mind that many people won’t be seeing your messages on a regular basis unless something is going wrong. So don’t put application critical information in it unless you have no choice.

Thanks for reading! 🙂

About The Author

Martyr2 is the founder of the Coders Lexicon and author of the new ebooks "The Programmers Idea Book" and "Diagnosing the Problem" . He has been a programmer for over 25 years. He works for a hot application development company in Vancouver Canada which service some of the biggest tech companies in the world. He has won numerous awards for his mentoring in software development and contributes regularly to several communities around the web. He is an expert in numerous languages including .NET, PHP, C/C++, Java and more.