NLog不写入事件日志.NET Core 2.1 [英] NLog not writing to eventlog .NET Core 2.1

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

问题描述

我已经将NLog添加到我的.NET核心控制台应用程序中,并且可以与文件和数据库目标一起使用.但是,当我尝试使其与eventviewer一起使用时,它不会记录任何内容.当我为eventviewer目标添加代码时,文件和数据库部分不记录任何内容.当我删除它时,日志记录将再次开始工作.

I've added NLog to my .NET core console app and it works with file and database targets. However when i try and get it to work with eventviewer it doesn't log anything. When i add the code for the eventviewer target the file and database part doesn't log anything. When I remove it, the logging starts working again.

我使用Powershell为该应用程序添加了新的事件查看器源,因此这不是问题.该应用程序不会崩溃或报告错误,它可以正常运行,但是在包含事件查看器时不记录任何内容.

I have added a new event viewer source for the application using Powershell, so that wouldn't be the issue. The application doesn't crash or report an error, it runs fine but doesn't log anything when event viewer is included.

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xsi:schemaLocation="NLog NLog.xsd"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  autoReload="true"
  internalLogFile="c:\temp\console-example-internal.log"
  internalLogLevel="Info" >

<targets>    
  <target xsi:type="File" name="logfile" fileName="c:\temp\console-example.log"
        layout="${date}|${level:uppercase=true}|${message} ${exception}|${logger}|${all-e
        vent-properties}" />

<target xsi:type="Console" name="console"
        layout="[${longdate}][${machinename}][${level:uppercase=true}] ${message} ${exception}" />

<target xsi:type="EventLog" name="eventlog" source="testlogging" log="Application"
        layout="${message}${newline}${exception:format=ToString}" />

  <target xsi:type="Database" name="database" >
      <connectionString>Server=test; Database=test; User Id=sa; Password=password;</connectionString>
      <commandText>
          INSERT INTO dbo.Log (Application, Logged, Level, Message, Logger, CallSite, Exception ) 
          VALUES (@Application, @Logged, @Level, @Message, @Logger, @Callsite, @Exception);
      </commandText>
      <parameter name="@application" layout="TestLoggingApp" />
      <parameter name="@logged" layout="${date}" />
      <parameter name="@level" layout="${level}" />
      <parameter name="@message" layout="url: ${aspnet-request-url} | action: ${aspnet-mvc-action} | ${message}" />

      <parameter name="@logger" layout="${logger}" />
      <parameter name="@callSite" layout="${callsite:filename=true}" />
      <parameter name="@exception" layout="${exception:tostring}" />
  </target>    
</targets>

<rules>
   <logger name="*" minlevel="Trace" writeTo="logfile, console, database" />        
</rules>
</nlog>

关于如何实施此功能或是否错过任何事情的任何想法?

Any ideas on how to implement this or if I have missed something?

推荐答案

对我来说,答案是将"NLog.WindowsEventLog"程序集添加到nlog.config的扩展区域:

For me the answer was adding "NLog.WindowsEventLog" assembly to the extensions area to nlog.config:

<extensions>
   <add assembly="NLog.WindowsEventLog" />
</extensions>

我具有默认值:"NLog.Web.AspNetCore".

I had the default: "NLog.Web.AspNetCore".

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

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