如何写入自定义事件日志?
我正在尝试让我的 .Net Windows 服务正确访问自定义事件日志。我使用 EventLogInstaller
在安装应用程序时创建事件日志和源。我在此处读到 Windows 需要一段时间才能注册源因此他们建议您在尝试写入日志之前重新启动应用程序。
由于这是一项 Windows 服务,我不想强制计算机重新启动或让用户手动启动该服务,因此我使用此代码等待日志存在,然后自动启动该服务。
while (!(EventLog.Exists("ManageIT") || EventLog.SourceExists("ManageIT Client Service")))
{
Thread.Sleep(1000);
}
System.ServiceProcess.ServiceController controller = new System.ServiceProcess.ServiceController("ManageIT.Client.Service");
controller.Start();
我的问题是,来自服务的事件仍然写入应用程序日志,尽管我可以在注册表编辑器中看到自定义日志,但它不会显示在 Windows 7 事件查看器中。
任何帮助将不胜感激。
I'm trying to get my .Net Windows Service to right to a custom event log. I'm using EventLogInstaller
to create the event log and source when the application is installed. I read here that it takes a while for Windows to register the source so they reccomend you restart the application before trying to write to the log.
As this is a Windows Service I didn't want to have to force a computer restart or get the user to manually start the service up, so I use this code to wait for the log to exist and then start the service automatically.
while (!(EventLog.Exists("ManageIT") || EventLog.SourceExists("ManageIT Client Service")))
{
Thread.Sleep(1000);
}
System.ServiceProcess.ServiceController controller = new System.ServiceProcess.ServiceController("ManageIT.Client.Service");
controller.Start();
My problem is that events from the service are still written to the Application Log and although I can see my custom log in the Registry Editor it does not show up in the Windows 7 Event Viewer.
Any help will be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
默认情况下,安装服务后,源会与应用程序日志关联。
如果我们稍后更改此关联,系统需要重新启动。
但是,我们可以通过在服务类(继承自 servicebase 的类)构造函数中将 autolog 属性设置为 false 来阻止服务与应用程序日志关联。
http://msdn.microsoft.com/en-us /library/system.serviceprocess.servicebase.autolog.aspx
By default when a service is installed, the source gets associated with the Application Log.
If we change this association at a later point, the system needs a restart.
We can however prevent the association of the service with the application log, by setting autolog property to false in the service class (class which inherits from servicebase) constructor.
http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.autolog.aspx
试试这个片段:
编辑 - 警告:如果运行代码的用户没有管理员权限,这将引发异常。由于这种情况(并且如果用户没有这些权限),最佳实践应该是假设日志存在,然后简单地写入它。请参阅:未找到来源,但无法搜索到部分或全部事件日志
Try this snippet:
edit - caveat: if the user running the code does not have administrator rights, this will throw an exception. Since this is the case (and if the user will not have these rights) best practices should be to assume the log exists, and simply write to it. see: The source was not found, but some or all event logs could not be searched
听起来您正在像这样写入事件日志:
这将写入应用程序日志。
如果您使用 simons post 中的代码创建 myLogger,则可以指定 Log 的名称。
It sounds like you are writing to the event log like this:
This will write to the application log.
If you use the code in simons post with the creation of myLogger, you can specify the name of the Log.
我做了这样的事情:
I did something like this: