写入 Windows 应用程序事件日志 [英] Write to Windows Application Event Log

查看:35
本文介绍了写入 Windows 应用程序事件日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法写入这个事件日志:

或者至少是其他一些 Windows 默认日志,我不必在其中注册事件源?

解决方案

是的,有一种方法可以写入您要查找的事件日志.您无需创建新源,只需使用现有源,该源通常与 EventLog 的名称相同,并且在某些情况下,例如事件日志应用程序,无需管理权限即可访问*.

*其他无法直接访问的情况,例如安全事件日志,只能由操作系统访问.

我使用此代码直接写入事件日志应用程序:

using (EventLog eventLog = new EventLog("Application")){eventLog.Source = "应用程序";eventLog.WriteEntry("日志消息示例", EventLogEntryType.Information, 101, 1);}

如您所见,EventLog 源与 EventLog 的名称相同.原因可以在 Event Sources @ Windows 中找到开发中心(我将引用源名称的部分加粗):

<块引用>

Eventlog 键中的每个日志都包含称为事件源的子键.事件源是记录事件的软件的名称.通常是应用程序的名称,如果应用程序很大,则是应用程序的子组件的名称.您最多可以向注册表添加 16,384 个事件源.

Is there a way to write to this event log:

Or at least, some other Windows default log, where I don't have to register an event source?

解决方案

Yes, there is a way to write to the event log you are looking for. You don't need to create a new source, just simply use the existent one, which often has the same name as the EventLog's name and also, in some cases like the event log Application, can be accessible without administrative privileges*.

*Other cases, where you cannot access it directly, are the Security EventLog, for example, which is only accessed by the operating system.

I used this code to write directly to the event log Application:

using (EventLog eventLog = new EventLog("Application")) 
{
    eventLog.Source = "Application"; 
    eventLog.WriteEntry("Log message example", EventLogEntryType.Information, 101, 1); 
}

As you can see, the EventLog source is the same as the EventLog's name. The reason of this can be found in Event Sources @ Windows Dev Center (I bolded the part which refers to source name):

Each log in the Eventlog key contains subkeys called event sources. The event source is the name of the software that logs the event. It is often the name of the application or the name of a subcomponent of the application if the application is large. You can add a maximum of 16,384 event sources to the registry.

这篇关于写入 Windows 应用程序事件日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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