NLog:在Action级别为Asp.Net Core添加参数

时间:2017-05-15 07:29:01

标签: logging parameters asp.net-core asp.net-core-mvc nlog

您好我根据this使用Nlog,我另外创建了一个自定义布局。在我的Action中,我想将参数传递给我可以在自定义布局中使用的Ilogger。在我的控制器动作中我有

var theEvent = new LogEventInfo(NLog.LogLevel.Debug, "", "");
theEvent.Properties["MyValue"] = "My custom string";
_logger.LogInformation("Index page says hello ", theEvent, 1, "two", 3.2);

但是当我调试自定义布局时,参数列表为空。参数也不会添加到属性列表中。我在这做错了什么

[LayoutRenderer("hello-universe")]
public class HelloUniverseLayoutRenderer : LayoutRenderer
{
    protected override void Append(StringBuilder builder, LogEventInfo logEvent)
    {       
        builder.Append("hello I am custom layout");
    }
}

enter image description here

1 个答案:

答案 0 :(得分:0)

目前唯一可行的方法是使用非DI记录器。

var logger = LogManager.GetLogger(YourController.GetType().FullName); //recommend as static field in your class

var theEvent = new LogEventInfo(NLog.LogLevel.Info, "", null, "your message with param {0}", "param1");
theEvent.Properties["MyValue"] = "My custom string";
logger.log(theEvent);