2011-02-02 64 views
7

我有一个安装项目,安装Windows服务。安装项目的Windows服务和事件日志

我们在winservice项目应该使用的自定义日志中注册事件日志源(如何以及为什么不重要)。

我的问题是,安装项目尝试创建默认情况下的事件日志源。这样做会得到一条错误消息("Error 1001" source XXX already exists on local computer)并回滚。

我已到处寻找,无法找到注册完成的位置或我可以关闭它。

如何强制Windows服务或安装项目不创建事件日志源?

回答

7

您可以删除默认EventLogInstaller

namespace MyService 
{ 
    [RunInstaller(true)] 
    public partial class ProjectInstaller : Installer 
    { 
     public ProjectInstaller() 
     { 
      InitializeComponent(); 

      // Remove the default Event Log Installer 
      EventLogInstaller DefaultInstaller = null; 
      foreach (Installer installer in serviceInstaller1.Installers) 
      { 
       if (installer is EventLogInstaller) 
       { 
        DefaultInstaller = (EventLogInstaller)installer; 
        break; 
       } 
      } 
      if (DefaultInstaller != null) 
      { 
       serviceInstaller1.Installers.Remove(DefaultInstaller); 
      } 
     } 
    } 
} 

或者,您可以修改Log属性:

foreach (Installer installer in serviceInstaller1.Installers) 
{ 
    if (installer is EventLogInstaller) 
    { 
     ((EventLogInstaller)installer).Log = "MyLog"; 
     break; 
    } 
} 

现在事件将成功记录到MyLog,并且服务启动/停止事件仍将记录到应用程序日志。

(来源:serviceInstaller component and its default EventLogInstaller

+0

注意,它的线,其中包括 “serviceInstaller1”`的foreach(在serviceInstaller1.Installers安装程序安装程序)`是很重要的。我把它写成`foreach(安装程序安装程序在this.Installers)`。这个建好,安装项目创建了MSI文件。但是当我尝试安装服务时,错误仍然存​​在。只有当我将“this.Installers”更改为“serviceInstaller1.Installers”时,才解决了错误。 – 2015-06-15 23:15:15

2

这只是一个基于我的测试的猜测。

安装程序项目(或WindowService类)会自动创建与myService.ServiceName同名的事件源。这很可能是因为每次启动/停止服务时,Start/Stop消息都会写入日志。这些消息需要一个源代码。

换句话说:您不需要像为您完成的那样创建与ServiceName同名的源。

3

我想当你卸载服务时,某些东西不能正确卸载,特别是在事件日志中。

要重新恢复到重新安装该服务的能力,我发现(thanks to this article),你需要在注册表中删除器(regedit.exe)此项:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\YOURSERVICENAME