使用Log.Info写出结构化日志记录很酷:
Log.Info("The record {id} has firstname {firstname} and lastname {lastname}",
record.Id, record.FirstName, record.LastName)我也经常使用Debug方法。在我开始使用messageGeneratorFunc变体之前,我用Log.IsDebugEnabled包装它们:
Log.Debug(() => string.Format("The record {0} has firstname {1} and lastname {2}",
record.Id, record.FirstName, record.LastName);我想知道如何在messageGeneratorFunc中使用结构化日志记录?
Log.Debug(() => Log.Debug("The record {id} has firstname {firstname} and lastname {lastname}",
record.Id, record.FirstName, record.LastName看起来很奇怪。Log.Debug Log.Debug,但当然可以工作...IsDebugEnabled的内部检查发生两次...有没有合适的替代方案,或者这应该是什么样子?
发布于 2019-01-09 05:09:16
messageGeneratorFunc委托用于重/大对象的专门序列化。未启用LogLevel时,NLog不会调用委托。
使用messageGeneratorFunc-delegate处理简单的消息模板是没有意义的。这实际上只会影响性能,因为它总是需要进行委托捕获,即使LogLevel没有启用。
这要便宜得多,而且更快:
Log.Debug("The record {0} has firstname {1} and lastname {2}",
record.Id, record.FirstName, record.LastName);
Log.Debug("The record {id} has firstname {firstname} and lastname {lastname}",
record.Id, record.FirstName, record.LastName);而不是这样做(去优化):
Log.Debug(() => string.Format("The record {0} has firstname {1} and lastname {2}",
record.Id, record.FirstName, record.LastName);另请参阅NLog教程:Logger should handle string formatting
https://stackoverflow.com/questions/54099317
复制相似问题