[转]Using NLog for ASP.NET Core to write custom information to the database

本文转自:https://github.com/NLog/NLog/issues/1366

In the previous versions of NLog it was easily possible to map custom log properties to custom database columns using LogEventInfo and ${event-properties} layout renderer:

LogEventInfo evt = new LogEventInfo(logLevel, "", message);
evt.Properties["custom"] = "some data";
log.Log(evt);

I cannot make this work using ASP.NET Core and NLog.Extensions.Logging.NLogLogger.

I am using standard ASP.NET Core logging infrastructure and NLog.Extensions.Logging package. At the runtime, my Microsoft.Extensions.Logging.ILogger contains additional NLog.Extensions.Logging.NLogLogger instance, configured to write all the events to the database. I want to pass some dictionary-like object to Microsoft.Extensions.Logging.ILogger.Log() to make this information available to NLog layout renderers. Is it supported?

 

 

Hi @dr-noise May be you can use the state parameter of Log method to send the dictionary but you should have to write your own ILogger implementation to write it in NLog logger.

Actually we can't handle this scenario because the ILogger interface is part of .NET Core and we can't extend the method signature

 

Thanks @phenixdotnet I'll have deeper look into this, I thought that state parameter already supported by the NLog.Extensions.Logging.NLogLogger.

For example, NLogLogger can check if state is instance of Dictionary<string, string> (or Dictionary<object, object>) and push all data from there to the underlying LogEventInfo.Properties.

 

I think we should do something about this is the near future.  The aspnet logging is prepared for structural logging, but NLog not.

Related http://nlog.userecho.com/topics/21-make-structural-logging-possible/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值