简体   繁体   English

记录Windows服务

[英]Logging Windows Service

I have the following code for logging my windows service which I found through research online. 我有以下代码用于记录通过在线研究发现的Windows服务。 its done inside of my service class right before it is being initialized like below. 如下所示,在初始化之前,它已经在我的服务类内部完成了。

public GBBInvService()
        {
            InitializeComponent();
            if (!System.Diagnostics.EventLog.SourceExists("MyLogSource"))
                System.Diagnostics.EventLog.CreateEventSource("MyLogSource",
                                                                      "MyDoLog");
            eventLog1.Source = "MyLogSource";
            eventLog1.Log = "MyDoLog";
        }

code for when logging: 记录时的代码:

eventLog1.WriteEntry("GBBInvService Service Started");

I have however been advised against it by our consultant. 但是,我们的顾问建议我不要这样做。 His comments below: 他的评论如下:

I'd avoid having the service create event sources at runtime. 我会避免让服务在运行时创建事件源。 This requires the service to run with high privileges (ie more access to the computer than is really necessary). 这要求该服务以高特权运行(即,对计算机的访问比实际需要的更多)。 Setting up the Windows event log sources is really an install-time job. 设置Windows事件日志源实际上是安装时的工作。 You may be able to add an event log installer to the project installer file (along with the service installer) and then the event source will always exist 您可能能够将事件日志安装程序添加到项目安装程序文件中(以及服务安装程序),然后事件源将始终存在

The problem with this advise is that I have not been able to find any examples where the logs are created in the project installer files. 该建议的问题在于,我无法找到在项目安装程序文件中创建日志的任何示例。 I also tried moving this log creation portion into my project installer but then it won't let me call or write to eventlog1 from my web service cs page. 我还尝试将日志创建部分移至我的项目安装程序中,但随后它不允许我从Web服务CS页面调用或写入eventlog1。 He has also suggested log4net but that is something that is new to me and pretty complicated to grasp a hold of. 他还建议使用log4net,但这对我来说是新事物,而且掌握起来相当复杂。 I'm still very new to windows services having just completed my first windows service project and would be very grateful for any sort of direction for creating logs in project installer, writing to it from my service cs page or any heads up on log4net. 我对刚刚完成我的第一个Windows服务项目的Windows服务还是很陌生,并且非常感谢在项目安装程序中创建日志,从服务CS页面写入日志或在log4net上进行任何提示的任何指导。

Your consultant is correct, it is a bad idea to do that within the service. 您的顾问是正确的,在服务中执行此操作是个坏主意。

Here's how you can create the event source in the installer: 您可以在安装程序中创建事件源的方法如下:

http://blogs.msdn.com/b/helloworld/archive/2008/12/11/creating-an-event-log.aspx?Redirected=true http://blogs.msdn.com/b/helloworld/archive/2008/12/11/creating-an-event-log.aspx?Redirected=true

Short version: Subclass Installer, and create the event log sources in the constructor of your subclassed installer. 简短版本:子类安装程序,并在子类安装程序的构造函数中创建事件日志源。

UPDATE 更新

From the link: 从链接:

After the service installer is executed, the log is 'registered', but not created yet. 执行服务安装程序后,日志已“注册”,但尚未创建。 To create it, one event must be written. 要创建它,必须编写一个事件。 If the service runs using a restricted user account, that account may not have enough security permission to write the first log, as the log need to be created. 如果服务使用受限用户帐户运行,则该帐户可能没有足够的安全权限来写入第一个日志,因为需要创建该日志。

The example code does not show one event being written. 示例代码未显示正在写入的一个事件。 Be sure you are doing that. 确保您正在这样做。

When installing the service, the user must run the installer as Administrator 安装服务时,用户必须以管理员身份运行安装程序

Make sure you ran the installer as Administrator. 确保以管理员身份运行安装程序。

From your comment: 根据您的评论:

Service wouldn't start. 服务无法启动。

Look in the Event Log for a reason it is not starting. 在事件日志中查找其未启动的原因。 Perhaps it is throwing an Exception, eg if you did not write the one event in the installer or if you did not run the installer as Administrator. 可能会引发异常,例如,如果您没有在安装程序中编写一个事件,或者您没有以管理员身份运行安装程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM