【问题标题】:NLog: can't write to event logNLog:无法写入事件日志
【发布时间】:2010-07-01 20:58:51
【问题描述】:

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

【问题讨论】:

  • 如果我以管理员身份运行我的应用程序,日志消息将正确显示在事件日志中。我使用的是 Windows 7。除非我以管理员身份运行我的应用程序,否则是否没有其他方法可以写入事件日志? (我偶然发现这篇文章给了我尝试以管理员身份运行的想法:webcache.googleusercontent.com/…

标签: logging event-log nlog


【解决方案1】:

来自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" 

【讨论】:

  • 不使用VBS,而是使用这个命令行。容易得多。 stackoverflow.com/questions/446691/… "eventcreate /ID 1 /L APPLICATION /T INFORMATION /SO MYEVENTSOURCE /D "我的第一个日志"
  • 我发现您只需以管理员身份运行 VS 一次,并在此过程中创建一个事件日志。之后您可以正常运行,它仍然可以工作(除非您更改 source 名称)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-08
  • 1970-01-01
相关资源
最近更新 更多