简体   繁体   English

NLog 在日志文件中包含 EventId

[英]NLog include EventId in log file

The ILogger in .NETCore 2.2 has a EventId parameter. .NETCore 2.2 中的 ILogger 有一个 EventId 参数。

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

How can I get NLog to output this to the log file?如何让 NLog 将其输出到日志文件?

My nlog.config:我的 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>

The issue: the EventId never makes it to the log file问题:EventId 永远不会出现在日志文件中

    |   | 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.

You could use你可以用

  • ${event-properties:EventId} - will print the name of EventId or if empty, then the id of EventId ${event-properties:EventId} -将打印的名称EventId或者空的,那么的ID EventId
  • ${event-properties:EventId_Id} - print the id ${event-properties:EventId_Id} - 打印 id
  • ${event-properties:EventId_Name} - print the name ${event-properties:EventId_Name} - 打印名称

Please note, empty Event ids aren't logged by default - that is, id = 0 or name if empty string or null.请注意,默认情况下不记录空事件 id - 即 id = 0 或名称(如果为空字符串或 null)。

If you need the empty Event id, then set the option IgnoreEmptyEventId to false in the setup:如果您需要空事件 ID,则在设置中将选项IgnoreEmptyEventId设置为false

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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM