C# Nlog with.NET core-如何在没有消息的情况下记录JSON对象
我正在使用Nlog和.NET内核以及依赖项注入来记录JSON对象。 我的日志当前看起来像:C# Nlog with.NET core-如何在没有消息的情况下记录JSON对象,c#,dependency-injection,.net-core,nlog,C#,Dependency Injection,.net Core,Nlog,我正在使用Nlog和.NET内核以及依赖项注入来记录JSON对象。 我的日志当前看起来像:{“log”:{“resourceid”:“2”}{“log”:{“resourceid”:“423”}并且我希望我的日志作为简单的JSON对象打印,而不带消息/属性名字段:{“resourceid”:“2”}{“resourceid”:“423”} 这是我的Nlog.config: 在我的Program.cs中,我在“Main”方法中添加了以下行: var factory=NLog.Web.NLogB
{“log”:{“resourceid”:“2”}{“log”:{“resourceid”:“423”}
并且我希望我的日志作为简单的JSON对象打印,而不带消息/属性名字段:{“resourceid”:“2”}{“resourceid”:“423”}
这是我的Nlog.config:
在我的Program.cs中,我在“Main”方法中添加了以下行:
var factory=NLog.Web.NLogBuilder.ConfigureNLog(“NLog.config”);
NLog.Config.ConfigurationItemFactory.Default.JsonConverter=new JsonNetSerializer();
var logger=factory.GetCurrentClassLogger();
my appSettings.json文件:
{
"Environment": 1,
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Trace",
"System": "Information",
"Microsoft": "Information"
}
}
}
这就是我记录属性的方式
_logger.LogInformation("{log}", input);
我必须添加一条消息作为
LogInformation
的第一个参数,我想删除它。尝试将占位符从{log}
重命名为{@resourceId}
并记录该属性:(不要忘记@sign)
我不确定,但您可能还需要从nlog.config
文件中删除以下行:
<attribute name="properties" layout="${json-event-properties}" encode="false"/>
什么是${json事件属性}
您是否尝试过这样启用maxRecursionLimit=“10”
:
<layout xsi:type="JsonLayout" includeAllProperties="True" suppressSpaces="True" maxRecursionLimit="10">
</layout>
<layout xsi:type="JsonLayout" suppressSpaces="True">
<attribute name="properties" encode="false">
<layout xsi:type="JsonLayout" includeAllProperties="True" suppressSpaces="True" maxRecursionLimit="10" />
</attribute>
</layout>
或者像这样:
<layout xsi:type="JsonLayout" includeAllProperties="True" suppressSpaces="True" maxRecursionLimit="10">
</layout>
<layout xsi:type="JsonLayout" suppressSpaces="True">
<attribute name="properties" encode="false">
<layout xsi:type="JsonLayout" includeAllProperties="True" suppressSpaces="True" maxRecursionLimit="10" />
</attribute>
</layout>
听起来像是:yoiu是否考虑过只为隐藏第一个参数的ILogger
创建自己的自定义扩展方法?您还可以创建自己的自定义LogState对象来实现IReadOnlyList: