gpt4 book ai didi

c# - CreateLogger() 之前被调用过,只能调用一次

转载 作者:行者123 更新时间:2023-12-02 02:02:32 28 4
gpt4 key购买 nike

我得到以下异常:

CreateLogger() was previously called and can only be called once.

我不知道什么时候 CreateLogger() 被调用了两次。

我的配置是这样的:

   public static void Main(string[] args)
{
try
{
var host = CreateHostBuilder(args).Build();
Log.Information("Starting host...");
host.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly."); //HERE IS THE EXCEPTION: CreateLogger() was previously called and can only be called once.
}
finally
{
Log.CloseAndFlush();
}
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.UseSerilog((provider, context, loggerConfig) =>
{
loggerConfig.Configure(provider, Configuration);
});
});

我在调用库中的扩展方法:

  public static void Configure(this LoggerConfiguration loggerConfig,
IServiceProvider provider, IConfiguration config)
{
var sqlserverConnectionString = config["ConnectionStrings:S1"];
var sqlserverLogTable = config["Logging:S2"];
var rollingFileName = config["Logging:S3"];

var sinkOpts = new MSSqlServerSinkOptions();
sinkOpts.TableName = sqlserverLogTable;
sinkOpts.AutoCreateSqlTable = true;
var columnOpts = new ColumnOptions();
columnOpts.Store.Remove(StandardColumn.Properties);
columnOpts.Store.Add(StandardColumn.LogEvent);
columnOpts.LogEvent.DataLength = 2048;
columnOpts.PrimaryKey = columnOpts.TimeStamp;
columnOpts.TimeStamp.NonClusteredIndex = true;


loggerConfig
.ReadFrom.Configuration(config)
.Enrich.FromLogContext()
.Enrich.WithMachineName()
.Enrich.WithAssemblyName()
.Enrich.WithAssemblyVersion()
.Enrich.WithAspnetcoreHttpcontext(provider, GetContextInfo)
.WriteTo.File(rollingFileName)
.WriteTo.MSSqlServer(
connectionString: sqlserverConnectionString,
sinkOptions: sinkOpts,
columnOptions: columnOpts
).CreateLogger();
}

最佳答案

UseSerilog calls CreateLogger internally ... 所以要么:

  • 不要自己调用 CreateLogger,让 UseSerilog 为您完成
  • UseSerilog 使用不同的重载,在其中为其提供您自己创建的 ILogger

关于c# - CreateLogger() 之前被调用过,只能调用一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68820411/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com