29

Since the recently introduced new structure of the Program.cs startup code, the documentation confuses me a bit.

In the officially provided Serilog.AspNetCore example and in the Serilog.Sentry example, they use .UseSerilog() on the WebHostBuilder. I cannot find this method.

This is what I have tried:

using Serilog;

var builder = WebApplication.CreateBuilder(args);
// adding services...

builder.Logging.AddSerilog(); // <- is this even necessary?

var app = builder.Build();
app.UseSerilogRequestLogging();

// configure request pipeline
app.Run();

But how / where can I configure the sinks, e.g. Debug, Console, Sentry, ...? I have the feeling that docs are a bit outdated or I am just a bit blind.

ˈvɔlə
  • 9,204
  • 10
  • 63
  • 89

4 Answers4

33

You'll need to make sure you have the following packages installed:

  • Serilog
  • Serilog.Extensions.Hosting (this provides the .UseSerilog extension method. If you have the Serilog.AspNetCore package, you do not need to explicitly include this)

Then you'll need a using:

using Serilog;

Which should allow you to access .UseSerilog via builder.Host:

using Serilog;

var builder = WebApplication.CreateBuilder(args);
builder.Host.UseSerilog();
var app = builder.Build();

app.MapGet("/", () => "Hello World!");

app.Run();

You can use a different overload to get the hosting context, services, and configuration. From there you can configure sinks, etc.:

builder.Host.UseSerilog((hostContext, services, configuration) => {
    configuration.WriteTo.Console();
});
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • 13
    Just as a sidenote, from the official GitHub Readme of `Serilog.Extensions.Hosting`: _ASP.NET Core applications should consider using `Serilog.AspNetCore` instead, which bundles this package and includes other ASP.NET Core-specific features._ . – ˈvɔlə Mar 24 '22 at 09:44
  • Thanks! I was missing the `builder.Host.UseSerilog();`! – ˈvɔlə Mar 24 '22 at 09:46
  • Great, manually adding `using Serilog;` did the trick. Before I would not get a intellisense suggestion for `.UseSerilog()` – Gabriel Weidmann Aug 11 '23 at 10:35
7

I used the following code snippet to fix the issue. I added it to Program.cs

var logger = new LoggerConfiguration()
.ReadFrom.Configuration(builder.Configuration)
.Enrich.FromLogContext()
.CreateLogger();

builder.Logging.ClearProviders();
builder.Logging.AddSerilog(logger);

link

SeeSharp
  • 608
  • 1
  • 13
  • 21
3

On top of Program.cs class paste the following:-

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console()
.WriteTo.File("logs/rumble-.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
Mahmmoud Kinawy
  • 551
  • 4
  • 11
-1
//serilog
Log.Logger = new LoggerConfiguration()
    
    .WriteTo.File(new JsonFormatter(),@"logs\log-.txt", rollingInterval: RollingInterval.Hour)
    .MinimumLevel.Error()
    .WriteTo.Console(new JsonFormatter())
    .CreateLogger();
builder.Host.UseSerilog(Log.Logger);
var app = builder.Build();
toyota Supra
  • 3,181
  • 4
  • 15
  • 19