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

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

我得到以下异常:

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。 Augusto Proiete 已经做了简短的解释...如果您想看一看,这里有一些 detail

您的代码在第 183 行执行,因为 useReload 变量应该为空,就好像您没有对记录器做任何事情一样,变量 reloadable 应该为假,因为它最初评估as ReloadableLogger reloadable = Log.Logger as ReloadableLogger;(屏幕后面没有直接显示在该文件上的内容)