![](/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.