在Windows服务程序中记录事件

编程入门 行业动态 更新时间:2024-10-27 14:17:54
本文介绍了在Windows服务程序中记录事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经创建了一个Windows服务程序,我希望我的错误严格写入Windows事件日志。所以我按照代码项目文章中的这些步骤:

I have created a Windows service program and I want my error to strictly be written to the Windows eventLog. So I followed these steps from code project article:

http ://www.codeproject/KB/dotnet/simplewindowsservice.aspx

但是我没有看到我写的任何自定义日志消息当我启动或停止服务时,在事件查看器窗口中创建的事件日志。 另外如何指定消息是由于错误还是仅仅是信息?

But I don't see any of the custom log messages I wrote in the event logs created in the event viewer window when I start or stop the service. Also how do I specify whether the message was due to an error or is just info?

推荐答案

首先, a href =msdn.microsoft/en-us/library/system.diagnostics.eventlog.aspx =noreferrer> MSDN 是您的朋友。确保您查看链接,因为有一些值得了解的潜在问题。

First, MSDN is your friend. Make sure you check out the link, as there are some potential gotchas worth knowing.

本质上,您创建一个EventLog对象:

Essentially, you create an EventLog object:

this.ServiceName = "MyService"; this.EventLog = new System.Diagnostics.EventLog(); this.EventLog.Source = this.ServiceName; this.EventLog.Log = "Application";

如果上述源不存在,您还需要创建源代码:

You also need to create a source, if the above source doesn't exist:

((ISupportInitialize)(this.EventLog)).BeginInit(); if (!EventLog.SourceExists(this.EventLog.Source)) { EventLog.CreateEventSource(this.EventLog.Source, this.EventLog.Log); } ((ISupportInitialize)(this.EventLog)).EndInit();

然后只需使用它:

this.EventLog.WriteEntry("My Eventlog message.", EventLogEntryType.Information);

其实很简单。

更多推荐

在Windows服务程序中记录事件

本文发布于:2023-11-15 17:34:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1596540.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:事件   程序   Windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!