NLog:无法写入事件日志

spa*_*rks 14 logging nlog event-log

我无法使用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>
Run Code Online (Sandbox Code Playgroud)

在事件查看器中,我正在查看"事件查看器(本地)">"Windows日志">"应用程序".但是,我在日志中看不到"aaaTest"(我定义的源代码)的实例.

Fab*_*nte 18

来自nlog论坛帖子

为了能够写入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" 
Run Code Online (Sandbox Code Playgroud)

  • 而不是使用 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”我的第一个日志” (2认同)