直到今天,我们一直在使用 NLog 版本 4.4.12(没有结构化日志记录)。但是,我们使用https://www.nuget.org/packages/NLog.StructuredLogging.Json/进行结构化日志记录。
使用此扩展的好处是您不需要模板化消息(包含索引或占位符以供您记录其他参数/对象)。该消息不包含任何索引或占位符供您记录的其他对象(即匿名类型)。
切换到支持开箱即用结构化日志记录的 NLog 4.6.5,我们希望摆脱那个额外的 NuGet 包。但是,仅当使用带有实际索引/命名占位符的模板化消息时,才会记录我们的附加参数。
我们的消息中没有索引或占位符确实会导致我们的其他参数/对象无法通过 JSON 呈现出来。
是否有可能有非模板化的消息,但仍然使用 NLog 的结构化日志记录我们已传递给它们以添加到 JSON 中的附加参数?
下面是一个示例(请注意,我们在 nlog 周围使用了一个额外的包装器)
NLog 版本:4.6.5
平台:.Net 4.5
当前的 NLog 配置
// Arrange
var typeUsingLogger = typeof(NLogWrapperTest);
var nLogWrapper = new NLogWrapper(typeof(NLogWrapper));
var level = (LogLevel)Enum.Parse(typeof(LogLevel), nLevel.Name);
var message = $"{Guid.NewGuid()}"; // {{extendedLogProperties}} {{@extendedLogProperties}} {{@purchase}} {{badplaceholder}}
var innerException = new DivideByZeroException("bla inner exception");
var exception = new ArgumentNullException("bla out exception", innerException);
var extendedLogProperties = new
{
ClientID = 8,
MyOtherProp = "abc",
MySubObject = new
{
//nested object although not recommended
A = 123,
B = "yep"
}
};
//log configuration
var logConfig = new LoggingConfiguration();
var memoryTarget = new MemoryTarget("MemoryTarget");
var jsonLayout = new JsonLayout
{
IncludeAllProperties = true,
Attributes =
{
new JsonAttribute("dateTime", "${date:universalTime=true:format=o}" ),
new JsonAttribute("level", "${level:uppercase=true}" ),
new JsonAttribute("logger", "${logger}" ),
new JsonAttribute("message", "${message}" ),
new JsonAttribute("callsite", "${callsite:className=true:methodName=true:skipFrame=0}" ),
new JsonAttribute("exception", "${exception:format=ToString:innerFormat=ToString}" ),
new JsonAttribute("machinename", "${machinename}" ),
new JsonAttribute("processid", "${processid}" ),
new JsonAttribute("threadid", "${threadid}" ),
new JsonAttribute("threadname", "${threadname}" ),
new JsonAttribute("application", "${application}" ),
new JsonAttribute("aspnetSessionId", "${aspnet-sessionid}" ),
new JsonAttribute("iisSiteName", "${iis-site-name}" ),
new JsonAttribute("stage", "${stage}" ),
}
};
memoryTarget.Layout = jsonLayout;
logConfig.AddTarget("memoryTarget", memoryTarget);
var memoryTargetLoggingRule = new LoggingRule("*", nLevel, memoryTarget);
logConfig.LoggingRules.Add(memoryTargetLoggingRule);
LogManager.Configuration = logConfig;
// Act
nLogWrapper.Log(level, message, typeUsingLogger, exception, extendedLogProperties);
var jsonLogMsg = memoryTarget.Logs[0];
Assert.Matches("ClientID", jsonLogMsg);
为什么我们需要它?
在没有任何替换索引或占位符的情况下保持消息不变真是太好了,这样我们就可以在日志中搜索完全相同的消息。(使用
new JsonAttribute("message", "${message:raw=true}"
不是一种选择)同样通过这种方式,我们最终不会在日志消息中包含 JSON 序列化对象(替换模板化消息的占位符/索引)以及这些附加参数的附加 JSON 字段。
请查看其最佳实践:https ://github.com/justeat/NLog.StructuredLogging.Json/blob/master/README.md#best-practices
如果你问:“为什么不继续使用 NuGet NLog 扩展?” 答案是,当在嵌套对象的模板化消息中使用 {@placeholder} 时,NLog 的结构化日志可以更好地呈现附加参数。
编辑 1:我想让我的匿名对象的所有属性都呈现在 json 的根目录中。如:
{
...
"ClientID": 8,
"MyOtherProp": "abc",
"MySubObject": {
"A": 123,
"B": "yep"
},
...
}