Configuring Serilog throws collection modified exception.

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

Configuring Serilog throws collection modified exception

问题

我正在创建一个.NET 6 Web API应用程序,并尝试在其中配置Serilog。以下是代码片段。执行时,我收到“集合已修改”异常。

  1. using Serilog;
  2. using Serilog.Core;
  3. using Serilog.Events;
  4. var ihb = Host.CreateDefaultBuilder(args)
  5. .ConfigureWebHostDefaults(webBuilder =>
  6. {
  7. webBuilder.ConfigureServices((context, collection) =>
  8. {
  9. collection.AddLogging(builder =>
  10. {
  11. var logger = new LoggerConfiguration().
  12. MinimumLevel.ControlledBy(new LoggingLevelSwitch(LogEventLevel.Error))
  13. // 其他配置在此处
  14. .CreateLogger();
  15. webBuilder.UseSerilog(logger);
  16. });
  17. });
  18. });
  19. try
  20. {
  21. var ih = ihb.Build();
  22. ih.Run();
  23. }
  24. catch (Exception ex) { }

有人可以指出可能的问题是什么吗?

谢谢。

英文:

I am creating .net 6 web api app and I am trying to configure serilog in it. Below is the snippet of the code. When executing, am getting collection was modified exception..

  1. using Serilog;
  2. using Serilog.Core;
  3. using Serilog.Events;
  4. var ihb = Host.CreateDefaultBuilder(args)
  5. .ConfigureWebHostDefaults(webBuilder =>
  6. {
  7. webBuilder.ConfigureServices((context, collection) =>
  8. {
  9. collection.AddLogging(builder =>
  10. {
  11. var logger = new LoggerConfiguration().
  12. MinimumLevel.ControlledBy(new LoggingLevelSwitch(LogEventLevel.Error))
  13. // other configuration goes here
  14. .CreateLogger();
  15. webBuilder.UseSerilog(logger);
  16. });
  17. });
  18. });
  19. try
  20. {
  21. var ih = ihb.Build();
  22. ih.Run();
  23. }
  24. catch (Exception ex) { }

Could someone point out what the issue might be..

Thanks..

答案1

得分: 1

为了解决这个问题,您可以修改您的代码以在AddLogging方法之外配置Serilog。

  1. using Serilog;
  2. using Serilog.Core;
  3. using Serilog.Events;
  4. var ihb = Host.CreateDefaultBuilder(args)
  5. .ConfigureWebHostDefaults(webBuilder =>
  6. {
  7. webBuilder.ConfigureServices((context, collection) =>
  8. {
  9. // 复制服务集合
  10. var services = collection.BuildServiceProvider();
  11. // 从服务集合中检索上下文对象
  12. var context = services.GetRequiredService<IWebHostEnvironment>();
  13. // 使用上下文对象进行日志配置
  14. var logger = new LoggerConfiguration()
  15. .MinimumLevel.ControlledBy(new LoggingLevelSwitch(LogEventLevel.Error))
  16. // 使用上下文对象进行其他配置
  17. .CreateLogger();
  18. // 使用配置好的日志记录器启用Serilog
  19. webBuilder.UseSerilog(logger);
  20. // 将日志服务添加到集合中
  21. collection.AddLogging();
  22. });
  23. });
  24. try
  25. {
  26. var ih = ihb.Build();
  27. ih.Run();
  28. }
  29. catch (Exception ex) { }
英文:

To resolve this issue, you can modify your code to configure Serilog outside the AddLogging method.

  1. using Serilog;
  2. using Serilog.Core;
  3. using Serilog.Events;
  4. var ihb = Host.CreateDefaultBuilder(args)
  5. .ConfigureWebHostDefaults(webBuilder =&gt;
  6. {
  7. webBuilder.ConfigureServices((context, collection) =&gt;
  8. {
  9. // Make a copy of the services collection
  10. var services = collection.BuildServiceProvider();
  11. // Retrieve the context object from the services collection
  12. var context = services.GetRequiredService&lt;IWebHostEnvironment&gt;();
  13. // Log configuration using the context object
  14. var logger = new LoggerConfiguration()
  15. .MinimumLevel.ControlledBy(new LoggingLevelSwitch(LogEventLevel.Error))
  16. // other configuration using the context object
  17. .CreateLogger();
  18. // Use Serilog with the configured logger
  19. webBuilder.UseSerilog(logger);
  20. // Add logging services to the collection
  21. collection.AddLogging();
  22. });
  23. });
  24. try
  25. {
  26. var ih = ihb.Build();
  27. ih.Run();
  28. }
  29. catch (Exception ex) { }

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

发表评论

匿名网友

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

确定