NLog:无法写入事件日志 [英] NLog: can't write to event log

查看:405
本文介绍了NLog:无法写入事件日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用NLog写入事件日志.我已经能够写入控制台和文件.我已在NLog中打开异常,并且没有收到NLog的反馈.

I can't write to the event log with NLog. I've been able to write to the console and to a file. I've turned on exceptions in NLog and am receiving no feedback from NLog.

这是我的NLog.config:

Here is my 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"(我定义的源)实例.

In Event Viewer, I am looking at "Event Viewer (Local)" > "Windows Logs" > "Application". However, I see no instances of "aaaTest" (my defined source) in the log.

推荐答案

来自要能够写入EventLog,必须将应用程序注册为事件源.如果您以管理员身份运行VS,则会自动发生.如果您创建安装程序并安装您的应用程序,则会被注册.

To be able to write to the EventLog an application must be registered as an event source. If you run VS as admin this happens automatically. If you create an installer and install your application it gets registered.

要手动将应用注册为事件源,我使用以下脚本:

To register an app manually as event source I use the following script:

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" 

这篇关于NLog:无法写入事件日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆