Core中的NLog中向日志消息添加属性

Core中的NLog中向日志消息添加属性

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

问题描述

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

我在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"] = "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);
 

然后,您将能够在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>
 

将NLog与AspNetCore一起使用时,为ASP添加 NLog.Web软件包非常有用.NET Core ,可为您提供许多预定义的布局渲染器.您可以在其Github页面上找到有关用于AspNetCore的NLog.Web 的更多信息.

此AspNetCore软件包将为您提供以下内容:

 <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
 

您可以在 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中向日志消息添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 05:18