您的位置:首页 > 编程语言 > ASP

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

2017-03-07 14:18 597 查看
本文转自: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/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐