I have developed a Windows service in C#. I have created a installer with Visual Studio 2008, which installs the Windows service. Everything is good so far. I want to make sure that the event source has been created at install time, so that any error/exception conditions at runtime are correctly logged to the Windows event log.
Does the event source get automatically created (and removed) as part of the windows service installation (and uninstallation), or do I have to handle this myself and create a custom action to create and delete it as follows?
protected override void OnBeforeInstall(IDictionary savedState)
{
base.OnBeforeInstall(savedState);
if (!EventLog.SourceExists(ServiceName))
EventLog.CreateEventSource(ServiceName, "Application");
}
protected override void OnAfterUninstall(IDictionary savedState)
{
base.OnAfterInstall(savedState);
if (EventLog.SourceExists(ServiceName))
EventLog.DeleteEventSource(ServiceName);
}