NLog在日志文件中包含EventId

时间:2019-06-21 19:14:49

标签: c# asp.net-core nlog

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

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

如何获取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"
        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从未进入日志文件

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

1 个答案:

答案 0 :(得分:1)

您可以使用

  • ${event-properties:EventId}-将打印EventId的名称,如果为空,则显示EventId的ID
  • ${event-properties:EventId_Id}-打印ID
  • ${event-properties:EventId_Name}-打印名称

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

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

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

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

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