我已经在C#中开发了Windows服务。我用Visual Studio 2008创建了一个安装程序,它安装了Windows服务。目前为止,一切都很好。我想确保在安装时创建事件源,以便运行时的任何错误/异常情况都能正确记录到Windows事件日志中。安装Windows服务时是否需要手动创建Windows事件日志源
是否事件源都会自动创建(删除)作为Windows服务安装(和卸载)的一部分,还是我来处理这个自己并创建一个自定义操作来创建和删除它,如下所示?
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);
}