NLog:无法写入事件日志

时间:2010-07-01 20:58:51

标签: logging event-log 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>

在事件查看器中,我正在查看“事件查看器(本地)”&gt; “Windows日志”&gt; “应用”。但是,我在日志中看不到“aaaTest”(我定义的源代码)的实例。

1 个答案:

答案 0 :(得分:18)

来自nlog forum post

为了能够写入EventLog,必须将应用程序注册为事件源。如果您以管理员身份运行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" 
相关问题