2010-07-01 160 views
14

我無法使用NLog寫入事件日誌。我已經能夠寫入控制檯和文件。我在NLog中打開了異常,並且沒有收到NLog的反饋。NLog:無法寫入事件日誌

這裏是我的NLog.config:

<?xml version="1.0" encoding="utf-8" ?> 
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     throwExceptions="true"> 
    <targets> 
     <target name="console" xsi:type="Console" layout="${message}" /> 
     <target xsi:type="EventLog" name="eventlog" layout="${message}" log="Application" source="aaaTest"/> 
     <target xsi:type="File" fileName="log.txt" name="file"/> 
    </targets> 

    <rules> 
     <logger name="*" minlevel="Debug" writeTo="eventlog,console,file" /> 
    </rules> 
</nlog> 

在事件查看器,我在看 「事件查看器(本地)」 - > 「Windows日誌」> 「應用程序」。但是,我沒有在日誌中看到「aaaTest」(我定義的源)的實例。

+0

如果我跑我的應用程序管轄,日誌消息將正確地在事件日誌中顯示出來。我正在使用Windows 7.除非將我的應用程序作爲管理運行,否則沒有其他方式可以寫入事件日誌嗎? (我偶然發現了這篇文章,它讓我有機會嘗試以管理身份運行:http://webcache.googleusercontent.com/search?q=cache:8pYpa9wBFTEJ:connect.microsoft.com/VisualStudio/feedback/details/509224/security -exception-trying-access-application-event-log + my.application.log + securityexception&cd = 1&hl = en&ct = clnk&gl = us&client = firefox -a) – sparks 2010-07-01 21:34:57

回答

18

nlog forum post

爲了能夠寫入事件日誌的應用程序必須註冊爲事件源。如果您以管理員身份運行VS,則會自動發生。如果您創建安裝程序並安裝您的應用程序,它將被註冊。

要註冊一個應用程序手動爲事件源我使用下面的腳本:

Set Args = WScript.Arguments 
If Args.Count < 1 then 
    WScript.Echo "USAGE: CreateEventSource.vbs <EventSourceName>" 
    WScript.Quit 
End If 
EventSourceName = Args(0) 

Set WshShell = WScript.CreateObject("WScript.Shell") 

'Create event source 
KeyName = "HKLM\SYSTEM\CurrentControlSet\Services\Eventlog\Application\" & EventSourceName & "\EventMessageFile" 
'Change path to .NET Framework version used 
WshShell.RegWrite KeyName,"%windir%\Microsoft.NET\Framework64\v2.0.50727\EventLogMessages.dll", "REG_EXPAND_SZ" 
+1

改爲使用此命令行代替VBS。更容易。 http://stackoverflow.com/questions/446691/how-to-create-windows-eventlog-source-from-command-line/1036133#1036133 「eventcreate/ID 1/L APPLICATION/T INFORMATION/SO MYEVENTSOURCE/D「我的第一個日誌」 – 2014-01-15 00:11:52

+0

我發現你只需要以管理員身份運行VS並在創建事件日誌的同時創建一個事件日誌,然後你可以像平常一樣運行,並且它仍然可以工作(除非你改變'source'名稱) 。 – SharpC 2018-02-05 17:09:43