Is it possible to pass some logging 'scope variables' to a .NET log message where these variables are not in the message?

huangapple go评论106阅读模式
英文:

Is it possible to pass some logging 'scope variables' to a .NET log message where these variables are not in the message?

问题

我希望向单个日志消息提供一些额外的元数据(也称为作用域变量)。

尽管日志消息没有与任何作用域变量的链接。那么我该如何做呢?

例如。

  1. var userId = 1;
  2. var userName = "Leia";
  3. _logger.LogInformation("Saved user.");

那么如何将 userIduserName 传递给该日志消息,但我不希望在该日志消息文本中打印这些值。

因为我正在使用语义日志记录(在后台/日志记录器中),我可以轻松地查看与每条日志消息关联的所有元数据。

这可行吗?

英文:

I wish to provide some extra meta data (aka scope variables) to a single logging message.

The logging message doesn't have link to any of the scope variables though. So how can I do this?

for example.

  1. var userId = 1;
  2. var userName = "Leia";
  3. _logger.LogInformation("Saved user.");

so how can I pass the userId and userName to that log message but i do NOT want to print those values in that log message text.

Because I'm using SEMANTIC LOGGING (behind the scenes/logger), I can easily see all the meta data associated to each log message.

Is this possible?

答案1

得分: 2

您可以尝试使用作用域。内置的日志记录基础结构和许多第三方日志记录提供程序使用此类似的抽象:

  1. var services = new ServiceCollection();
  2. services.AddLogging(builder => builder.AddJsonConsole(opts => opts.IncludeScopes = true));
  3. var sp = services.BuildServiceProvider();
  4. var logger = sp.GetRequiredService<ILogger<Tests>>();
  5. using var _ = logger.BeginScope(new Dictionary<string, object>
  6. {
  7. { "ScopedId", 1 }
  8. });
  9. logger.LogError("Test");

输出消息:

  1. {
  2. "EventId":0,
  3. "LogLevel":"Error",
  4. "Category":"NET7Tests.Tests",
  5. "Message":"Test",
  6. "State":{
  7. "Message":"Test",
  8. "{OriginalFormat}":"Test"
  9. },
  10. "Scopes":[
  11. {
  12. "Message":"System.Collections.Generic.Dictionary\u00602[System.String,System.Object]",
  13. "ScopedId":1
  14. }
  15. ]
  16. }

阅读更多:

英文:

You can try using scopes. Build-in logging infrastructure and many 3rd-party logger providers use this or similar abstraction:

  1. var services = new ServiceCollection();
  2. services.AddLogging(builder =&gt; builder.AddJsonConsole(opts=&gt; opts.IncludeScopes = true));
  3. var sp = services.BuildServiceProvider();
  4. var logger = sp.GetRequiredService&lt;ILogger&lt;Tests&gt;&gt;();
  5. using var _ = logger.BeginScope(new Dictionary&lt;string, object&gt;
  6. {
  7. { &quot;ScopedId&quot;, 1 }
  8. });
  9. logger.LogError(&quot;Test&quot;);

Output message:

  1. {
  2. &quot;EventId&quot;:0,
  3. &quot;LogLevel&quot;:&quot;Error&quot;,
  4. &quot;Category&quot;:&quot;NET7Tests.Tests&quot;,
  5. &quot;Message&quot;:&quot;Test&quot;,
  6. &quot;State&quot;:{
  7. &quot;Message&quot;:&quot;Test&quot;,
  8. &quot;{OriginalFormat}&quot;:&quot;Test&quot;
  9. },
  10. &quot;Scopes&quot;:[
  11. {
  12. &quot;Message&quot;:&quot;System.Collections.Generic.Dictionary\u00602[System.String,System.Object]&quot;,
  13. &quot;ScopedId&quot;:1
  14. }
  15. ]
  16. }

Read more:

huangapple
  • 本文由 发表于 2023年6月19日 23:15:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76507960.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定