[英]Using C# params with serilog
Just starting using Serilog + ElasticSearch and was wondering if there is an elegant way to log a params
object array in one log entry. 刚开始使用Serilog + ElasticSearch,想知道是否有一种优雅的方法可以在一个日志条目中记录
params
对象数组。 So far the only way I have been able to manage it is looping through each params
which creates a separate log entry for each one. 到目前为止,我能够管理它的唯一方法是遍历每个
params
,从而为每个params
创建一个单独的日志条目。 Any way to combine them into one log entry? 有什么方法可以将它们组合成一个日志条目?
Thanks! 谢谢!
Sample: 样品:
public static void MethodEntry<T>(string methodName, params object[] parameters)
{
if (parameters.Length > 0)
foreach (var param in parameters) // Will create parameters.Length number of log entries
Log.ForContext(typeof(T)).Debug("Entering {MethodName} with {@Param}", methodName, param);
else
Log.ForContext(typeof(T)).Debug("Entering {MethodName}", methodName);
}
EDIT: 编辑:
Sinks used: 使用的水槽:
If you know the specific type of your Sender object you can use the following feature of Serilog to avoid logging of the not required information: 如果知道Sender对象的特定类型,则可以使用Serilog的以下功能来避免记录不需要的信息:
Log.Logger = new LoggerConfiguration()
.Destructure.ByTransforming<YOUR_SENDER_TYPE>(
r => new { firstValue = r.firstValue, secondValue = r.secondValue })
.WriteTo ....
More about logging structured data you can find in the official documentation Serilog Structured Data 有关记录结构化数据的更多信息,请参见官方文档Serilog结构化数据
public static void MethodEntry<T>(string methodName, params object[] parameters)
{
Log.ForContext<T>()
.ForContext("Parameters", parameters)
.Debug("Entering {MethodName}", methodName);
}
This should do what you're after. 这应该做您想要做的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.