将属性添加到 .net core 中 NLog 中的日志消息 [英] Adding properties to log message in NLog in .net core

查看:35
本文介绍了将属性添加到 .net core 中 NLog 中的日志消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 NLog 在 .net core 中记录消息.

我在 StartUp.cs 中添加了 NLog 如下:

loggerFactory.AddNLog();

为了记录到文件,我使用以下方法:

logger.LogInformation("Message");

我想将自定义 NLog 事件属性添加到我的消息中.但是 LogInformation() 不允许我通过它.我该怎么做?

解决方案

如果你想要自定义布局属性(NLog 称之为布局渲染器),你可以使用 EventProperties 布局渲染器.您可以简单地在代码中执行此操作:

var logger = LogManager.GetCurrentClassLogger();var eventInfo = new LogEventInfo(LogLevel.Info, logger.Name, "Message");eventInfo.Properties["CustomValue"] = "我的自定义字符串";eventInfo.Properties["CustomDateTimeValue"] = new DateTime(2020, 10, 30, 11, 26, 50);//你也可以像这样添加它们:eventInfo.Properties.Add("CustomNumber", 42);//发送到日志logger.Log(eventInfo);

然后您将能够在您的 nlog.config 中添加这些(您构成的任何属性)

<parameter name="@customtime" layout="${event-properties:CustomDateTimeValue:format=yyyy-MM-dd HH:mm:ss}"/><parameter name="@customvalue" layout="${event-properties:item=CustomValue}"/><参数名称="@customnumber" layout="${event-properties:item=CustomNumber}"/></目标>

在 AspNetCore 中使用 NLog 时,添加 NLog.Web Package for ASP 很有用.NET Core 为您提供了许多预定义的布局渲染器.您可以在他们的 Github 页面上找到有关 NLog.Web for AspNetCore 的更多信息.

这个 AspNetCore 包将为您提供如下内容:

<参数名称="@MvcAction" layout="${aspnet-MVC-Action}"/><parameter name="@Session" layout="${aspnet-session:Variable=User.Name:EvaluateAsNestedProperties=true}"/>... 等等

您可以在 NLog.Web.AspNetCore Github 页面上找到完整列表.>

I am using NLog for logging messages in .net core.

I have added NLog in StartUp.cs as follows:

loggerFactory.AddNLog();

For logging to file, I am using following method:

logger.LogInformation("Message");

I want to add custom NLog event properties to my message. But LogInformation() is not allowing me to pass that. How can I do that?

解决方案

If you want custom layout properties (NLog calls them layout renderers) you can use the EventProperties Layout Renderer. You can simply do this in your code:

var logger = LogManager.GetCurrentClassLogger();
var eventInfo = new LogEventInfo(LogLevel.Info, logger.Name, "Message");
eventInfo.Properties["CustomValue"] = "My custom string";
eventInfo.Properties["CustomDateTimeValue"] = new DateTime(2020, 10, 30, 11, 26, 50);
// You can also add them like this:
eventInfo.Properties.Add("CustomNumber", 42);
// Send to Log
logger.Log(eventInfo);

Then you will be able to add these (any any properties you make up) in your nlog.config

<target>
  <parameter name="@customtime" layout="${event-properties:CustomDateTimeValue:format=yyyy-MM-dd HH:mm:ss}" />
  <parameter name="@customvalue" layout="${event-properties:item=CustomValue}" />
  <parameter name="@customnumber" layout="${event-properties:item=CustomNumber}" />
</target>

When using NLog with AspNetCore, it's useful to add the NLog.Web Package for ASP.NET Core which gives you many predefined Layout renderers. You can find more about NLog.Web for AspNetCore on their Github page.

This AspNetCore package will give you things like the following:

<parameter name="@UserName" layout="${aspnet-user-identity}" />
<parameter name="@MvcAction" layout="${aspnet-MVC-Action}" />
<parameter name="@Session" layout="${aspnet-session:Variable=User.Name:EvaluateAsNestedProperties=true}" />
... etc

You will find the complete list on the NLog.Web.AspNetCore Github page.

这篇关于将属性添加到 .net core 中 NLog 中的日志消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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