Configuring Serilog throws collection modified exception.

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

Configuring Serilog throws collection modified exception

问题

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

using Serilog;
using Serilog.Core;
using Serilog.Events;

var ihb = Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
    webBuilder.ConfigureServices((context, collection) =>
    {
        collection.AddLogging(builder =>
        {
            var logger = new LoggerConfiguration().
                MinimumLevel.ControlledBy(new LoggingLevelSwitch(LogEventLevel.Error))
                // 其他配置在此处
                .CreateLogger();
    
            webBuilder.UseSerilog(logger);
        });
    });
});

try
{
  var ih = ihb.Build();
  ih.Run();
}
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..

using Serilog;
using Serilog.Core;
using Serilog.Events;

var ihb = Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
    webBuilder.ConfigureServices((context, collection) =>
    {
        collection.AddLogging(builder =>
        {
            var logger = new LoggerConfiguration().
                MinimumLevel.ControlledBy(new LoggingLevelSwitch(LogEventLevel.Error))
                //  other configuration goes here
                .CreateLogger();
    
            webBuilder.UseSerilog(logger);
        });
    });
});

try
{
  var ih = ihb.Build();
  ih.Run();
}
catch (Exception ex) { } 

Could someone point out what the issue might be..

Thanks..

答案1

得分: 1

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

using Serilog;
using Serilog.Core;
using Serilog.Events;

var ihb = Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder =>
    {
        webBuilder.ConfigureServices((context, collection) =>
        {
            // 复制服务集合
            var services = collection.BuildServiceProvider();

            // 从服务集合中检索上下文对象
            var context = services.GetRequiredService<IWebHostEnvironment>();

            // 使用上下文对象进行日志配置
            var logger = new LoggerConfiguration()
                .MinimumLevel.ControlledBy(new LoggingLevelSwitch(LogEventLevel.Error))
                // 使用上下文对象进行其他配置
                .CreateLogger();

            // 使用配置好的日志记录器启用Serilog
            webBuilder.UseSerilog(logger);

            // 将日志服务添加到集合中
            collection.AddLogging();
        });
    });

try
{
    var ih = ihb.Build();
    ih.Run();
}
catch (Exception ex) { }
英文:

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

using Serilog;
using Serilog.Core;
using Serilog.Events;

var ihb = Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder =&gt;
    {
        webBuilder.ConfigureServices((context, collection) =&gt;
        {
            // Make a copy of the services collection
            var services = collection.BuildServiceProvider();

            // Retrieve the context object from the services collection
            var context = services.GetRequiredService&lt;IWebHostEnvironment&gt;();

            // Log configuration using the context object
            var logger = new LoggerConfiguration()
                .MinimumLevel.ControlledBy(new LoggingLevelSwitch(LogEventLevel.Error))
                // other configuration using the context object
                .CreateLogger();

            // Use Serilog with the configured logger
            webBuilder.UseSerilog(logger);

            // Add logging services to the collection
            collection.AddLogging();
        });
    });

try
{
    var ih = ihb.Build();
    ih.Run();
}
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:

确定