Art*_*ous 6 c# logging string-interpolation serilog formattablestring
如何替换此代码:
string name = "John";
logger.Information("length of name '{name}' is {nameLength}", name, name.Length);
Run Code Online (Sandbox Code Playgroud)
像这样或类似的 C# 字符串插值
string name = "John";
// :-( lost benefit of structured logging: property names not passed to logger
logger.Information($"length of name '{name}' is {name.Length}");
Run Code Online (Sandbox Code Playgroud)
但保持结构化日志的属性名称工作?
将此文件添加到您的项目中。它有ILogger
扩展方法VerboseInterpolated()
等等DebugInterpolated()
。这里也有单元测试。
string name = "John";
// add 'Interpolated' to method name: InformationInterpolated() instead of Information()
// add name of the property after the expression. Name is passed to the logger
logger.InformationInterpolated($"length of name '{name:name}' is {name.Length:Length}");
Run Code Online (Sandbox Code Playgroud)
但要小心:很容易使用错误的方法。例如,如果您不小心使用了 Serilog 的方法,logger.Debug($"length = {length:propertyNameForLogger}")
它将记录length = propertyNameForLogger
,因此不会记录任何参数值。这是由于您的值的格式propertyNameForLogger
所致。
string name = "John";
// add 'Interpolated' to method name: InformationInterpolated() instead of Information()
// create an anonymous object with 'new { propertyName }'.
// It's much slower because of using Reflection, but allows to write the variable name only once.
logger.InformationInterpolated($"length of name '{new { name }}' is {new { name.Length }}");
// you can also specify other property names
logger.InformationInterpolated($"length of name '{new { userName = name }}' is {new { lengthOfName = name.Length }}");
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2628 次 |
最近记录: |