【问题标题】:CreateEventSource doesn't create event log entry and windows service can't be launchedCreateEventSource 不创建事件日志条目并且无法启动 windows 服务
【发布时间】:2020-06-11 04:25:44
【问题描述】:

我正在开发一个新的 Windows 服务,并按照instruction 中提供的步骤进行操作。

在服务类中,我做的和文章差不多

static EventLog _eventLog;

public CMSMetadata()
{
    InitializeComponent();

    _eventLog = new EventLog();
    if (!EventLog.SourceExists("CMSMetadata_Processing"))
    {
        EventLog.CreateEventSource(new EventSourceCreationData("CMSMetadata_Processing", "CMSMetadata"));
    }
    _eventLog.Source = "CMSMetadata_Processing";
    _eventLog.Log = "CMSMetadata";
}

protected override void OnStart(string[] args)
{
    _eventLog.WriteEntry("In OnStart.", EventLogEntryType.Information);
}

在我安装并尝试启动服务后,它显示以下错误

错误 1053:服务没有响应启动或控制 及时提出要求

我还注意到CreateEventSource() 在事件查看器中查找时不会创建事件日志条目。

我发现 this SO post 正在讨论我面临的 1053 错误,但没有一个解决方案适合我。

我已经确认/尝试过

  1. 服务是按发布模式构建的。
  2. InstallUtilManagedInstallerClass.InstallHelper() 安装
  3. 框架版本和我安装的一致,其实我 尝试了 4.5.2 和 4.7.2 以防万一它真的与 框架。
  4. 服务作为本地系统运行。
  5. 配置没问题。
  6. 如果我删除所有与事件日志相关的代码,则服务可以成功启动。

如果服务无法正确创建事件日志条目,我认为可以,我可以提前创建条目作为解决方法。

但是,这样一来,我什至无法安装该服务。 下面的安装日志 (CMSMetadata.InstallLog) 表明无论我是否使用CreateEventSource(),安装都会以某种方式创建事件source

Installing assembly 'C:\temp\GisSoftware\CMSMetadata\CMSMetadata.exe'.
Affected parameters are:
   logtoconsole = 
   logfile = C:\temp\GisSoftware\CMSMetadata\CMSMetadata.InstallLog
   assemblypath = C:\temp\GisSoftware\CMSMetadata\CMSMetadata.exe
Installing service CMSMetadata...
Service CMSMetadata has been successfully installed.
Creating EventLog source CMSMetadata in log Application...
Rolling back assembly 'C:\temp\GisSoftware\CMSMetadata\CMSMetadata.exe'.
Affected parameters are:
   logtoconsole = 
   logfile = C:\temp\GisSoftware\CMSMetadata\CMSMetadata.InstallLog
   assemblypath = C:\temp\GisSoftware\CMSMetadata\CMSMetadata.exe
Restoring event log to previous state for source CMSMetadata.
Service CMSMetadata is being removed from the system...
Service CMSMetadata was successfully removed from the system.

结束我的问题,在 Windows 服务中使用事件日志时我错过了什么?

【问题讨论】:

标签: c# windows-services event-log


【解决方案1】:

似乎该服务默认会创建一个事件日志。在生成Installer 之前,我可能应该将Service 的AutoLog 属性设置为false

ServiceInstaller 中找到EventLogInstaller 并修改日志和源解决了我的问题(还记得删除服务ctor 中的事件日志条目创建)。

public ProjectInstaller()
{
    InitializeComponent();

    EventLogInstaller installer = this.Installers.OfType<ServiceInstaller>().First()
        .Installers.OfType<EventLogInstaller>().First();

    installer.Source = _source;
    installer.Log = _logName;
}

此外,在安装过程中创建事件日志似乎比服务ctor 更好,因为似乎LocalSystem 访问注册表的权限是有限的。

参考:Easiest language for creating a Windows service

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-20
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    • 2020-09-24
    • 2013-10-10
    • 1970-01-01
    • 2016-05-24
    相关资源
    最近更新 更多