Question: I am developing a windows application in VB.NET for Windows Server 2008. Is there an easy way to create a customized log subview that can be read in the server’s Event Viewer by our client’s supervisor? In this case my app’s event messages could appear in its own “Applications and Services Logs” view in one group.
Answer: The Event Viewer is an MMC snap-in that enables you to browse and manage event logs. The Event Viewer in Windows Server 2008 was a real improvement. Using Event Viewer, the supervisor can easily automate responses to the application’s events as the Event Viewer is integrated with Task Scheduler. You can collect these events from the remote server and store them locally by specifying event subscriptions. Writing application events into Event Log should be a standard error logging practice for any application. It’s a great tool for developers.
In Microsoft .Net 2.0+ applications the EventLog class lets you access, create or customize Windows 2008 event logs to record information about your own important application events. You can create a log by adding your own event source (i.e. event group) to the system.
The event source registers your application with the event log as a valid source of new entries. The name of the event source must be unique. It is the common practice to use the name of the application.
There is a few seconds latency time to enable the event source and during the process administrator privileges may be required, so it should be created prior to executing your application. The best solution is to add it to the installation processes. Call EventLog.CreateEventSource to create your own custom event source:
Add to your application and call it from your code whenever you want:
' Create the custom Event View source, if it does not already exist.
' It should be created prior to executing the application that uses the source.
Public Sub InstallEventLog()
If Not EventLog.SourceExists("Name of My Event View") Then
EventLog.CreateEventSource("Name of My Event View", "MyAppEvents")
Console.WriteLine("Creating Custome Event Log")
'The source is created.
Catch ex As Exception