我正在尝试使用
访问服务器上的“ForwardedEvents”事件日志el = new EventLog("ForwardedEvents", serverName);
这不起作用。
我认为它不起作用,因为日志不包含在Eventlog希望找到它的注册表中(HKLM / System / CurrentControlSet / Services / Eventlog / ..)。
如何将日志添加到注册表以便找到它,或者是否有另一种方法来访问未在该位置指定的日志?
答案 0 :(得分:10)
通过为Log at创建新的注册表项来解决此问题: (HKEY_LOCAL_MACHINE \系统\ CurrentControlSet \服务\事件日志\ 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)
答案 1 :(得分:4)
这与此处提供的其他注册表解决方案很接近,但这是我在Windows 7上执行此操作的方式,并将写入应用程序日志,而不是转发事件日志:
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密钥。
答案 2 :(得分:3)
如果您仍希望以编程方式执行此操作,而不是通过注册表手动创建日志,则有一种方法。您需要先检查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.");