服务安装失败,因为事件源已经存在 [英] Service installation fails as event source already exists

查看:192
本文介绍了服务安装失败,因为事件源已经存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将基于Topshelf的Windows服务配置为使用Topshelf.Log4Net和log4net登录到自定义事件日志.如果我在命令行模式下运行该应用程序,则可以正常工作.当我尝试使用BillsTestService.exe install安装服务时,我得到:

I'm trying to configure a Topshelf-based Windows service to log to a custom event log using Topshelf.Log4Net and log4net. This works fine if I run the application in command-line mode. When I try to install the service with BillsTestService.exe install, I get:

INFO  Topshelf v3.1.107.0, .NET Framework v4.0.30319.18052
DEBUG Attempting to install 'BillsTestService'
Running a transacted installation.
...
Service BillsTestService has been successfully installed.
Creating EventLog source BillsTestService in log Application...

An exception occurred during the Install phase.
System.ArgumentException: Source BillsTestService already exists on the local computer.
...
   at System.Diagnostics.EventLog.CreateEventSource(EventSourceCreationData sourceData)

在安装之前,我试过在LINQPad中运行EventLog.DeleteEventSource("BillsTestService");;成功,但是随后的服务安装仍然失败.

I've tried running EventLog.DeleteEventSource("BillsTestService"); in LINQPad before installing; that succeeds, but a subsequent service install still fails.

我的log4net附加程序配置为:

My log4net appender configuration is:

<appender name="ErrorEventLogAppender" type="log4net.Appender.EventLogAppender" >
  <threshold value="ERROR" />
  <logName value="MyCompanyServices" />
  <applicationName value="BillsTestService" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%-5level %logger - %message%newline" />
  </layout>
</appender>

我在做什么错了?

目的是使多个服务日志错误指向相同的日志名称(具有不同的应用程序名称);该日志将由Operations创建.

The intent is to have multiple services log errors to the same log name (with different application names); the log would be created by Operations.

推荐答案

部分问题在于,Topshelf在安装时会自动创建一个以该服务命名的事件日志源.由于log4net附加程序applicationName也用作事件日志源,因此不能是实际的应用程序/服务名称.源在本地计算机上必须是唯一的.我在log4net配置的名称中添加了源"后缀.

Part of the issue is that Topshelf automatically creates an eventlog source named after the service when you install. Since the log4net appender applicationName is also used as an eventlog source, that cannot be the actual application/service name. The source must be unique on the local computer. I added a "Source" suffix to the name in the log4net configuration.

另一部分是该服务无权创建日志.它可以创建新的源,但不能创建新的日志.一种方法是在代码中(我使用LINQPad):

The other part is that the service does not have rights to create the log. It can create a new source, but not a new log. One way to do this is in code (I used LINQPad):

EventLog.CreateEventSource("FOODEBUG", "MyCoSvc");

EventLog mylog = new EventLog("MyCoSvc");
mylog.Source = "FOODEBUG";

mylog.WriteEntry("This is a test.");

EventLog.DeleteEventSource("FOODEBUG");

如果您实际上必须写日志来创建它,我不是很肯定.在此上花了两天多的时间后,我宁愿保持安全.

I'm not positive if you actually have to write to the log to create it; after spending over two days on this, I'd rather be safe.

还请注意,日志名称最多只能包含8个字符.您可以花更长的时间,但系统只会将前8个字符视为有效字符.

Also note that log names are limited to 8 characters; you can go longer, but the system only considers the first 8 characters as significant.

无需像Chris Patterson所建议的那样移动log4net初始化.简单地包括

There's no need to move the log4net initialization as Chris Patterson suggested. Simply including

configurator.DependsOnEventLog();
configurator.UseLog4Net("MyService.exe.config");

HostFactory.Run委托中的

就足够了. (我正在使用Topshelf.Log4Net.)

in the HostFactory.Run delegate is sufficient. (I'm using Topshelf.Log4Net.)

最后,我有把握地确定整个Windows事件日志记录系统都是不稳定的.并非在所有情况下都无法使用事件查看器的刷新功能,有一次我的应用程序日志条目消失了,并且我相信重启后会看到不同的结果.

Finally, I'm reasonably sure that the entire Windows event logging system is flaky. Event Viewer's refresh doesn't work in all cases, at one point my Application log entries disappeared, and I believe I've seen different results after a reboot.

这篇关于服务安装失败,因为事件源已经存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆