gpt4 book ai didi

c# - NLog 在日志文件中包含 EventId

转载 作者:行者123 更新时间:2023-12-04 16:42:33 31 4
gpt4 key购买 nike

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

最佳答案

你可以使用

  • ${event-properties:EventId} - 将打印 EventId 的名称或者如果为空,则 EventId 的 id
  • ${event-properties:EventId_Id} - 打印身份证
  • ${event-properties:EventId_Name} - 打印名字

请注意,默认情况下不记录空事件 id - 即 id = 0 或 name 如果为空字符串或 null。

如果您需要空的 Event id,则设置选项 IgnoreEmptyEventIdfalse在设置中:

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

关于c# - NLog 在日志文件中包含 EventId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56709247/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com