Skip to content

Commit 843a569

Browse files
authored
#10 - keep BuildWebHost() in example code [Skip CI]
1 parent e5031e9 commit 843a569

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

README.md

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Install-Package Serilog.AspNetCore -DependencyVersion Highest
1212
Install-Package Serilog.Sinks.Console
1313
```
1414

15-
**Next**, in your application's _Program.cs_ file, configure Serilog first:
15+
**Next**, in your application's _Program.cs_ file, configure Serilog first. A `try`/`catch` block will ensure any configuration issues are appropriately logged:
1616

1717
```csharp
1818
public class Program
@@ -25,25 +25,11 @@ public class Program
2525
.Enrich.FromLogContext()
2626
.WriteTo.Console()
2727
.CreateLogger();
28-
```
29-
30-
Then, add `UseSerilog()` to the web host builder. A `try`/`catch` block will ensure any configuration issues are appropriately logged:
3128

32-
```csharp
3329
try
3430
{
3531
Log.Information("Starting web host");
36-
37-
var host = new WebHostBuilder()
38-
.UseKestrel()
39-
.UseContentRoot(Directory.GetCurrentDirectory())
40-
.UseIISIntegration()
41-
.UseStartup<Startup>()
42-
.UseSerilog() // <-- Add this line
43-
.Build();
44-
45-
host.Run();
46-
32+
BuildWebHost(args).Run();
4733
return 0;
4834
}
4935
catch (Exception ex)
@@ -56,6 +42,16 @@ Then, add `UseSerilog()` to the web host builder. A `try`/`catch` block will ens
5642
Log.CloseAndFlush();
5743
}
5844
}
45+
```
46+
47+
Then, add `UseSerilog()` to the web host builder in `BuildWebHost()`.
48+
49+
```csharp
50+
public static IWebHost BuildWebHost(string[] args) =>
51+
WebHost.CreateDefaultBuilder(args)
52+
.UseStartup<Startup>()
53+
.UseSerilog() // <-- Add this line
54+
.Build();
5955
}
6056
```
6157

@@ -83,6 +79,8 @@ That's it! With the level bumped up a little you will see log output like:
8379

8480
Tip: to see Serilog output in the Visual Studio output window when running under IIS, select _ASP.NET Core Web Server_ from the _Show output from_ drop-down list.
8581

82+
A more complete example, showing _appsettings.json_ configuration, can be found in [the sample project here](https://github.com/serilog/serilog-aspnetcore/tree/dev/samples/SimpleWebSample).
83+
8684
### Using the package
8785

8886
With _Serilog.AspNetCore_ installed and configured, you can write log messages directly through Serilog or any `ILogger` interface injected by ASP.NET. All loggers will use the same underlying implementation, levels, and destinations.

0 commit comments

Comments
 (0)