NLog MappedDiagnosticsLogicalContext.SetScoped 用于多个键
NLog MappedDiagnosticsLogicalContext.SetScoped for multiple keys
处理队列中的消息时,我想跟踪每条消息的多个参数 - 它的 ID、user_name 等
根据我的发现 MappedDiagnosticsLogicalContext.SetScoped
应该可以,有没有办法一次设置多个键?此类字段的数量根据已知条件而有所不同,因此我想要一个可以一次设置所有字段的助手。
不幸的是,当前 API 不允许组合,因此以下尝试失败:
public static IDisposable SetContext(this Dictionary<string, string> fieldValues)
{
IDisposable result = null;
foreach (var pair in fieldValues)
{
result = MappedDiagnosticsLogicalContext.SetScoped(pair.Key, pair.Value);
}
return result;
}
此方法仅删除最后添加的密钥,之前的所有密钥都保留在那里。
SetScoped
方法的底层 API 是 NLog 内部的,所以我不能轻易调用它们。
关于如何让它发挥作用有什么建议吗?
正在升级到 NLog 版本。 4.6.5 将允许您使用此方法:
IDisposable MappedDiagnosticsLogicalContext.SetScoped(IReadOnlyList<KeyValuePair<string,object>> items);
你可以这样称呼它:
MappedDiagnosticsLogicalContext.SetScoped(fieldValues);
但是你的字段值必须是Dictionary<string, object>
类型
处理队列中的消息时,我想跟踪每条消息的多个参数 - 它的 ID、user_name 等
根据我的发现 MappedDiagnosticsLogicalContext.SetScoped
应该可以,有没有办法一次设置多个键?此类字段的数量根据已知条件而有所不同,因此我想要一个可以一次设置所有字段的助手。
不幸的是,当前 API 不允许组合,因此以下尝试失败:
public static IDisposable SetContext(this Dictionary<string, string> fieldValues)
{
IDisposable result = null;
foreach (var pair in fieldValues)
{
result = MappedDiagnosticsLogicalContext.SetScoped(pair.Key, pair.Value);
}
return result;
}
此方法仅删除最后添加的密钥,之前的所有密钥都保留在那里。
SetScoped
方法的底层 API 是 NLog 内部的,所以我不能轻易调用它们。
关于如何让它发挥作用有什么建议吗?
正在升级到 NLog 版本。 4.6.5 将允许您使用此方法:
IDisposable MappedDiagnosticsLogicalContext.SetScoped(IReadOnlyList<KeyValuePair<string,object>> items);
你可以这样称呼它:
MappedDiagnosticsLogicalContext.SetScoped(fieldValues);
但是你的字段值必须是Dictionary<string, object>