1

我正在用 C# (.NET 4) 编写 Windows 服务。

这是安装程序的代码:

 [RunInstaller(true)]
public partial class JobManagerInstaller : Installer
{
    public JobManagerInstaller()
    {
        InitializeComponent();

        this.Installers.Clear();

        ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller();
        ServiceInstaller serviceInstaller = new ServiceInstaller();
        EventLogInstaller eventLogInstaller = new EventLogInstaller();

        // Service Account Information
        serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
        serviceProcessInstaller.Username = null;
        serviceProcessInstaller.Password = null;

        // Service Information
        // The installer's ServiceName must be identical to the JobManager.ServiceName set in the constructor of JobManager.cs
        serviceInstaller.ServiceName = "VIAVista";
        serviceInstaller.DisplayName = "VIAVista";
        serviceInstaller.StartType = ServiceStartMode.Automatic;

        // EventLog
        eventLogInstaller.Source = "VIAVista";
        eventLogInstaller.Log = "VIAVista";

        // Dependency SQL Server service (i.e.SQL Server must run)
        serviceInstaller.ServicesDependedOn = new string[] { "MSSQL$SQLEXPRESS" };

        this.Installers.Add(serviceProcessInstaller);
        this.Installers.Add(serviceInstaller);
        this.Installers.Add(eventLogInstaller);
    }
}

如您所见,我希望将事件的源和日志命名为“VIAVista”。

当我尝试在我的服务器(Windows Web Server 2008 R2 64bit)上安装服务时,我被告知事件源已经存在于日志“应用程序”中。这很奇怪,因为我认为 this.Installers.Clear() 会阻止创建默认源/日志。

信息:在安装服务之前,我使用 regedit 确保没有“VIAVista”密钥。

有任何想法吗?我错过了什么吗?

4

2 回答 2

2

尝试

     serviceInstaller.Installers.Clear(); 
于 2012-08-30T18:07:48.977 回答
1

做这个:

if (!EventLog.SourceExists(source))
    EventLog.CreateEventSource(source, log);

EventLog.WriteEntry(source, message, type, eventid);

如果源存在,它将被使用,否则将被创建。

于 2012-08-30T17:28:50.050 回答