Home > database >  Configure Serilog with WebApplicationBuilder in Net 6
Configure Serilog with WebApplicationBuilder in Net 6

Time:11-28

I Net Core 5 I used to configure Serilog using the recommended approach:

public static void Main(string[] args) {

  Log.Logger = new LoggerConfiguration().WriteTo.Console().CreateLogger();
    
  try {
    Log.Information("Starting up");
    CreateHostBuilder(args).Build().Run();
  }
  catch (Exception ex) {
    Log.Fatal(ex, "Application start-up failed");
  }
  finally {
    Log.CloseAndFlush();
  }

}

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

How to configure Serilog in Net 6 using WebApplicationBuilder:

var builder = WebApplication.CreateBuilder(args);

// Configure Serilog

var app = builder.Build();

CodePudding user response:

You can still use the same extension method on Host.

using Serilog;

var builder = WebApplication.CreateBuilder(args);

builder.Host.UseSerilog();

var app = builder.Build();
  • Related