NLog在日志文件中包含EventId [英] NLog include EventId in log file

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

问题描述

.NETCore 2.2中的ILogger具有EventId参数.

The ILogger in .NETCore 2.2 has a EventId parameter.

public static void LogError(this ILogger logger, EventId eventId, string message, params object[] args);

如何获取NLog将此输出到日志文件?

How can I get NLog to output this to the log file?

我的nlog.config:

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"
        autoReload="true">
    <extensions>
        <add assembly="NLog.Web.AspNetCore"/>
    </extensions>
    <targets>
        <target xsi:type="File" name="f" fileName="${aspnet-appbasepath}/Logs/${shortdate}.log"            
                layout=" ${event-properties:item=EventId.Id} ${eventId} ${eventId.Id} | ${longdate} ${uppercase:${level}} ${callsite}  ${message}" />
        </targets>
    <rules>
        <logger name="*" minlevel="Info" writeTo="f" />
    </rules>
</nlog>

问题:EventId从未进入日志文件

The issue: the EventId never makes it to the log file

    |   | 2019-06-21 13:13:44.8984 INFO RMD_LPCB_Web_Portal.Program.Main  init main
    |   | 2019-06-21 13:13:48.5197 WARN Microsoft.AspNetCore.HttpsPolicy.Internal.HttpsLoggingExtensions.FailedToDeterminePort  Failed to determine the https port for redirect.
    |   | 2019-06-21 13:13:54.3064 INFO RMD_LPCB_Web_Portal.Pages.Account.LoginModel+<AuthenticateEmail>d__20.MoveNext  LOGIN : Jesse.lay@state.nm.us IP: ::1 SESSION : f52d9758-bc32-7131-d7d4-7ec5224853f5
    |   | 2019-06-21 13:13:54.4070 INFO RMD_LPCB_Web_Portal.Pages.Account.LoginModel.AuthenticateActiveDirectory  Jesse.lay AD
    |   | 2019-06-21 13:13:54.4432 INFO RMD_LPCB_Web_Portal.Pages.Account.LoginModel.AuthenticateActiveDirectory  Jesse.lay AD logged in
    |   | 2019-06-21 13:13:54.4432 INFO RMD_LPCB_Web_Portal.Pages.Account.LoginModel+<AuthenticateEmail>d__20.MoveNext  LOGIN : Jesse.lay@state.nm.us IP: ::1 SESSION : f52d9758-bc32-7131-d7d4-7ec5224853f5
    |   | 2019-06-21 13:13:54.4603 INFO RMD_LPCB_Web_Portal.Pages.Account.LoginModel+<OnPostAsync>d__17.MoveNext  User Jesse.Lay@state.nm.us logged in at 6/21/2019 7:13:54 PM.
    |   | 2019-06-21 13:13:57.2319 ERROR RMD_LPCB_Web_Portal.Pages.Eoc.IndexModel.OnGet     at RMD_LPCB_Web_Portal.Pages.Eoc.IndexModel.OnGet() in C:\usr\tfs_Workspace\TSSB\RMD LPCB\Apps\RMD LPCB Web Portal\Source\RMD_LPCB_Web_Portal\Pages\Eoc\Index.cshtml.cs:line 44
    |   | 2019-06-21 13:13:58.6918 ERROR Microsoft.AspNetCore.Diagnostics.Internal.DiagnosticsLoggerExtensions.UnhandledException  An unhandled exception has occurred while executing the request.

推荐答案

您可以使用

  • ${event-properties:EventId}-将显示EventId的名称,如果为空,则显示EventId
  • 的ID
  • ${event-properties:EventId_Id}-打印ID
  • ${event-properties:EventId_Name}-打印名称
  • ${event-properties:EventId} - will print the name of EventId or if empty, then the id of EventId
  • ${event-properties:EventId_Id} - print the id
  • ${event-properties:EventId_Name} - print the name

请注意,默认情况下不会记录空的事件ID-即id = 0或名称(如果为空字符串或null).

Please note, empty Event ids aren't logged by default - that is, id = 0 or name if empty string or null.

如果您需要空的事件ID,则在设置中将选项IgnoreEmptyEventId设置为false:

If you need the empty Event id, then set the option IgnoreEmptyEventId to false in the setup:

loggerFactory.AddNLog(new NLogProviderOptions { IgnoreEmptyEventId = false });

为完整起见,一些记录器可能会调用:

To be complete, some possible logger calls:

logger.LogError(new EventId(2, "eventId2"), "Message with event id number and event id name");
logger.LogError(new EventId(2), "Message with only event id number");
logger.LogError(2, "Message with only event id number");

这篇关于NLog在日志文件中包含EventId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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