• [转]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/

  • 相关阅读:
    第三周学习进度条
    软件工程个人作业02
    构建之法阅读笔记02
    学习进度条
    构建之法阅读笔记01
    软件工程个人作业01
    构建之法粗读
    第一次作业
    动手动脑接口与继承
    大道至简第七章第八章
  • 原文地址:https://www.cnblogs.com/freeliver54/p/6514516.html
Copyright © 2020-2023  润新知