![](/img/trans.png)
[英]Why does my Event Log source keep getting put under “Application” in the registry instead of <log>?
[英]add event log to registry
我正在尝试使用服务器访问服务器上的“ForwardedEvents”事件日志
el = new EventLog("ForwardedEvents", serverName);
这不起作用。
我相信它不起作用,因为日志不包含在Eventlog期望找到它的注册表中(HKLM / System / CurrentControlSet / Services / Eventlog / ..)。
如何将日志添加到注册表以便找到它,或者是否有另一种方法来访问未在该位置指定的日志?
通过为Log at:(HKEY_LOCAL_MACHINE \\ SYSTEM \\ CurrentControlSet \\ services \\ eventlog \\ LOGNAME)创建新的注册表项来解决此问题。
这是通过..(在Windows Server 2008 R2上)..
1)右键单击父文件夹(eventlog) - >新建 - >键
2)将密钥命名为在(C:\\ Windows \\ System32 \\ winevt \\ Logs \\ LOGNAME)中找到的evtx文件
3)在注册表资源管理器的右窗格中,右键单击 - >新建 - >可扩展字符串值
4)命名新创建的REG_EXPAND_SZ“文件”
5)右键单击名称“文件”
6)修改
7)在“数值数据”框中,添加evtx文件的路径,如
(%SystemRoot%\\ System32 \\ winevt \\ Logs \\ ForwardedEvents.evtx)
这与此处提供的其他注册表解决方案很接近,但这是我在Windows 7上执行此操作的方式,并且将写入应用程序日志,而不是Forwarded Events日志:
Windows徽标>在搜索中键入regedit
,然后按Enter键
展开HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\services\\eventlog
找到Application
键并为您的应用程序创建一个新密钥: MyApp
在MyApp
,右键单击空白区域中的右侧窗口,然后选择“ 新建”>“可扩展字符串值” 。 这将创建一个REG_EXPAND_SZ
条目。 将其命名为EventMessageFile
。
双击新条目以设置值。 对于该值,请输入: C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\EventLogMessages.dll
选择“ 确定” 。
保留(Default)
字符串值,使其(value not set)
值。
通过将CurrentControlSet
替换为ControlSet001
和ControlSet002
重复两次。
如果您需要将应用程序移动到另一台计算机,则可以右键单击该键并选择“ Export
。 您将文件另存为.reg
文件,然后将其复制到下一台计算机。 在那里,双击运行它(以管理员身份登录)。 通过这种方式,您不必手动重新创建它,对于其他应用程序,您可以在记事本中实际编辑.reg
文件,只需更改应用程序的名称,保存即可(请务必将格式更改为“所有文件”,因此它在末尾保留.reg
,而不是将其保存为.txt
文件),然后您可以双击它以运行并插入新应用程序的EventLog键。
如果您仍然希望以编程方式执行此操作,而不是通过注册表手动创建日志,则有一种方法。 您需要先检查并查看EventSource
存在,如果不存在则需要创建它。 这必须在您尝试使用该源创建EventLog
实例之前发生。 请注意创建和使用之间的延迟,因此请务必处理此问题(有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/2awhba7a(v=vs.110).aspx )。
// Create the source, if it does not already exist.
if(!EventLog.SourceExists("MySource"))
{
//An event log source should not be created and immediately used.
//There is a latency time to enable the source, it should be created
//prior to executing the application that uses the source.
//Execute this sample a second time to use the new source.
EventLog.CreateEventSource("MySource", "MyNewLog");
Console.WriteLine("CreatedEventSource");
Console.WriteLine("Exiting, execute the application a second time to use the source.");
// The source is created. Exit the application to allow it to be registered.
return;
}
// Create an EventLog instance and assign its source.
EventLog myLog = new EventLog();
myLog.Source = "MySource";
// Write an informational entry to the event log.
myLog.WriteEntry("Writing to event log.");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.