英文:
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 =>
{
webBuilder.ConfigureServices((context, collection) =>
{
// Make a copy of the services collection
var services = collection.BuildServiceProvider();
// Retrieve the context object from the services collection
var context = services.GetRequiredService<IWebHostEnvironment>();
// 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) { }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论