Skip to content

Commit a04f096

Browse files
authored
Condense the alternative configuration example in the README [Skip CI]
1 parent 55d9d67 commit a04f096

File tree

1 file changed

+11
-35
lines changed

1 file changed

+11
-35
lines changed

README.md

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -89,41 +89,17 @@ With _Serilog.AspNetCore_ installed and configured, you can write log messages d
8989

9090
**Tip:** change the minimum level for `Microsoft` to `Warning` and plug in this [custom logging middleware](https://github.com/datalust/serilog-middleware-example/blob/master/src/Datalust.SerilogMiddlewareExample/Diagnostics/SerilogMiddleware.cs) to clean up request logging output and record more context around errors and exceptions.
9191

92-
### Alternative configuration
92+
### Inline initialization
9393

94-
You can chose to build the logger as part of the `WebHostBuilder` pipeline, and thus benefit from the application configuration.
95-
The following code shows an example of such a configuration:
94+
You can alternatively configure Serilog using a delegate as shown below:
9695

97-
````csharp
98-
public class Program
99-
{
100-
public static void Main(string[] args)
101-
{
102-
var host = new WebHostBuilder()
103-
.UseKestrel()
104-
.UseContentRoot(Directory.GetCurrentDirectory())
105-
// Load the application configuration over the web host configuration.
106-
.ConfigureAppConfiguration((hostingContext, configurationBuilder) =>
107-
{
108-
configurationBuilder
109-
.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
110-
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
111-
.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true)
112-
.AddEnvironmentVariables();
113-
})
114-
// Configure Serilog to be used as the logger for the whole application.
115-
.UseSerilog((hostingContext, loggerConfiguration) =>
116-
loggerConfiguration.ReadFrom.Configuration(hostingContext.Configuration)
117-
.Enrich.FromLogContext()
118-
.WriteTo.Console()
119-
)
120-
.UseIISIntegration()
121-
.UseStartup<Startup>()
122-
.Build();
123-
124-
host.Run();
125-
}
126-
}
127-
````
96+
```csharp
97+
.UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
98+
.ReadFrom.Configuration(hostingContext.Configuration)
99+
.Enrich.FromLogContext()
100+
.WriteTo.Console())
101+
```
102+
103+
This has the advantage of making the `hostingContext`'s `Configuration` object available for configuration of the logger, but at the expense of recording `Exception`s raised earlier in program startup.
128104

129-
With this code, the default behavior is to set the created `ILogger` as the default logger. `Log.Logger` can be used as usual to access the created logger.
105+
If this method is used, `Log.Logger` is assigned implicitly, and closed when the app is shut down.

0 commit comments

Comments
 (0)