14

我无法使用 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”(我定义的源)的实例。

4

1 回答 1

18

来自nlog 论坛帖子

为了能够写入事件日志,应用程序必须注册为事件源。如果您以管理员身份运行 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" 
于 2011-01-13T11:37:57.183 回答