Skip to content

Commit d44e684

Browse files
authored
Merge pull request #87 from nblumhardt/readme-updates
Update samples and README to use recent .NET host APIs
2 parents 4f6a463 + 4d2de16 commit d44e684

File tree

5 files changed

+98
-148
lines changed

5 files changed

+98
-148
lines changed

README.md

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
Serilog logging for _Microsoft.Extensions.Hosting_. This package routes framework log messages through Serilog, so you can get information about the framework's internal operations written to the same Serilog sinks as your application events.
44

5-
**ASP.NET Core** applications should consider [using _Serilog.AspNetCore_ instead](https://github.com/serilog/serilog-aspnetcore), which bundles this package and includes other ASP.NET Core-specific features.
5+
**Versioning:** This package tracks the versioning and target framework support of its
6+
[_Microsoft.Extensions.Hosting_](https://nuget.org/packages/Microsoft.Extensions.Hosting) dependency. Most users should choose the version of _Serilog.Extensions.Hosting_ that matches
7+
their application's target framework. I.e. if you're targeting .NET 7.x, choose a 7.x version of _Serilog.Extensions.Hosting_. If
8+
you're targeting .NET 8.x, choose an 8.x _Serilog.Extensions.Hosting_ version, and so on.
69

710
### Instructions
811

@@ -13,46 +16,37 @@ dotnet add package Serilog.Extensions.Hosting
1316
dotnet add package Serilog.Sinks.Console
1417
```
1518

16-
**Next**, in your application's _Program.cs_ file, configure Serilog first. A `try`/`catch` block will ensure any configuration issues are appropriately logged:
19+
**Next**, in your application's _Program.cs_ file, configure Serilog first. A `try`/`catch` block will ensure any configuration issues are appropriately logged. Call `AddSerilog()` on the host application builder:
1720

1821
```csharp
19-
public class Program
22+
using Serilog;
23+
24+
Log.Logger = new LoggerConfiguration()
25+
.Enrich.FromLogContext()
26+
.WriteTo.Console()
27+
.CreateLogger();
28+
29+
try
2030
{
21-
public static int Main(string[] args)
22-
{
23-
Log.Logger = new LoggerConfiguration()
24-
.MinimumLevel.Debug()
25-
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
26-
.Enrich.FromLogContext()
27-
.WriteTo.Console()
28-
.CreateLogger();
29-
30-
try
31-
{
32-
Log.Information("Starting host");
33-
BuildHost(args).Run();
34-
return 0;
35-
}
36-
catch (Exception ex)
37-
{
38-
Log.Fatal(ex, "Host terminated unexpectedly");
39-
return 1;
40-
}
41-
finally
42-
{
43-
Log.CloseAndFlush();
44-
}
45-
}
46-
```
31+
Log.Information("Starting host");
4732

48-
**Then**, add `UseSerilog()` to the host builder in `BuildHost()`.
33+
var builder = Host.CreateApplicationBuilder(args);
34+
builder.Services.AddHostedService<PrintTimeService>();
35+
builder.Services.AddSerilog();
4936

50-
```csharp
51-
public static IHost BuildHost(string[] args) =>
52-
new HostBuilder()
53-
.ConfigureServices(services => services.AddSingleton<IHostedService, PrintTimeService>())
54-
.UseSerilog() // <- Add this line
55-
.Build();
37+
var app = builder.Build();
38+
39+
await app.RunAsync();
40+
return 0;
41+
}
42+
catch (Exception ex)
43+
{
44+
Log.Fatal(ex, "Host terminated unexpectedly");
45+
return 1;
46+
}
47+
finally
48+
{
49+
await Log.CloseAndFlushAsync();
5650
}
5751
```
5852

@@ -88,7 +82,3 @@ You can alternatively configure Serilog using a delegate as shown below:
8882
This has the advantage of making the `hostingContext`'s `Configuration` object available for configuration of the logger, but at the expense of ignoring `Exception`s raised earlier in program startup.
8983

9084
If this method is used, `Log.Logger` is assigned implicitly, and closed when the app is shut down.
91-
92-
### Versioning
93-
94-
This package tracks the versioning and target framework support of its [_Microsoft.Extensions.Hosting_](https://nuget.org/packages/Microsoft.Extensions.Hosting) dependency.

samples/SimpleServiceSample/Program.cs

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,39 @@
22
using Microsoft.Extensions.DependencyInjection;
33
using Microsoft.Extensions.Hosting;
44
using Serilog;
5+
using SimpleServiceSample;
56

6-
namespace SimpleServiceSample;
7+
Log.Logger = new LoggerConfiguration()
8+
.Enrich.FromLogContext()
9+
.WriteTo.Console()
10+
.CreateBootstrapLogger();
711

8-
public static class Program
12+
try
913
{
10-
public static int Main(string[] args)
11-
{
12-
Log.Logger = new LoggerConfiguration()
13-
.Enrich.FromLogContext()
14-
.WriteTo.Console()
15-
.CreateBootstrapLogger();
14+
Log.Information("Getting the motors running...");
1615

17-
try
18-
{
19-
Log.Information("Getting the motors running...");
20-
CreateHostBuilder(args).Build().Run();
21-
return 0;
22-
}
23-
catch (Exception ex)
24-
{
25-
Log.Fatal(ex, "Host terminated unexpectedly");
26-
return 1;
27-
}
28-
finally
29-
{
30-
Log.CloseAndFlush();
31-
}
32-
}
16+
var builder = Host.CreateApplicationBuilder(args);
17+
18+
builder.Services.AddHostedService<PrintTimeService>();
3319

34-
public static IHostBuilder CreateHostBuilder(string[] args) =>
35-
Host.CreateDefaultBuilder(args)
36-
.ConfigureServices(services => services.AddHostedService<PrintTimeService>())
37-
.UseSerilog((context, services, loggerConfiguration) => loggerConfiguration
38-
.ReadFrom.Configuration(context.Configuration)
39-
.ReadFrom.Services(services)
40-
.Enrich.FromLogContext()
41-
.WriteTo.Console());
20+
builder.Services.AddSerilog((services, loggerConfiguration) => loggerConfiguration
21+
.ReadFrom.Configuration(builder.Configuration)
22+
.ReadFrom.Services(services)
23+
.Enrich.FromLogContext()
24+
.WriteTo.Console());
25+
26+
var app = builder.Build();
27+
28+
await app.RunAsync();
29+
30+
return 0;
31+
}
32+
catch (Exception ex)
33+
{
34+
Log.Fatal(ex, "Host terminated unexpectedly");
35+
return 1;
36+
}
37+
finally
38+
{
39+
await Log.CloseAndFlushAsync();
4240
}
Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,45 @@
11
using System;
2+
using Microsoft.AspNetCore.Builder;
23
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.AspNetCore.Http;
35
using Microsoft.Extensions.Hosting;
46
using Serilog;
57

6-
namespace WebApplicationSample;
78

8-
public static class Program
9-
{
10-
public static int Main(string[] args)
11-
{
12-
Log.Logger = new LoggerConfiguration()
13-
.WriteTo.Console()
14-
.CreateBootstrapLogger();
9+
Log.Logger = new LoggerConfiguration()
10+
.WriteTo.Console()
11+
.CreateBootstrapLogger();
1512

16-
Log.Information("Starting up!");
13+
Log.Information("Starting up!");
14+
15+
try
16+
{
17+
var builder = WebApplication.CreateBuilder();
1718

18-
try
19-
{
20-
CreateHostBuilder(args).Build().Run();
19+
builder.Services.AddSerilog((services, loggerConfiguration) => loggerConfiguration
20+
.WriteTo.Console()
21+
.ReadFrom.Configuration(builder.Configuration)
22+
.ReadFrom.Services(services));
2123

22-
Log.Information("Stopped cleanly");
23-
return 0;
24-
}
25-
catch (Exception ex)
26-
{
27-
Log.Fatal(ex, "An unhandled exception occured during bootstrapping");
28-
return 1;
29-
}
30-
finally
31-
{
32-
Log.CloseAndFlush();
33-
}
34-
}
24+
var app = builder.Build();
3525

36-
public static IHostBuilder CreateHostBuilder(string[] args) =>
37-
Host.CreateDefaultBuilder(args)
38-
.UseSerilog((context, services, configuration) => configuration
39-
.WriteTo.Console()
40-
.ReadFrom.Configuration(context.Configuration)
41-
.ReadFrom.Services(services))
42-
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
43-
}
26+
app.MapGet("/", () =>
27+
{
28+
Log.Information("Saying hello");
29+
return "Hello World!";
30+
});
31+
32+
await app.RunAsync();
33+
34+
Log.Information("Stopped cleanly");
35+
return 0;
36+
}
37+
catch (Exception ex)
38+
{
39+
Log.Fatal(ex, "An unhandled exception occured during bootstrapping");
40+
return 1;
41+
}
42+
finally
43+
{
44+
await Log.CloseAndFlushAsync();
45+
}

samples/WebApplicationSample/Startup.cs

Lines changed: 0 additions & 41 deletions
This file was deleted.

samples/WebApplicationSample/appsettings.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
"MinimumLevel": {
44
"Default": "Information",
55
"Override": {
6-
"Microsoft": "Warning",
7-
"Microsoft.Hosting.Lifetime": "Information"
6+
"Microsoft.AspNetCore.Hosting": "Information",
7+
"Microsoft.AspNetCore.Mvc": "Warning",
8+
"Microsoft.AspNetCore.Routing": "Warning"
89
}
910
},
1011
"WriteTo": [

0 commit comments

Comments
 (0)