@@ -28,41 +28,41 @@ using Serilog;
2828
2929public class Startup
3030{
31- public Startup (IHostingEnvironment env )
32- {
33- Log .Logger = new LoggerConfiguration ()
34- .Enrich .FromLogContext ()
35- .WriteTo .Console ()
36- .CreateLogger ();
37-
38- // Other startup code
31+ public Startup (IHostingEnvironment env )
32+ {
33+ Log .Logger = new LoggerConfiguration ()
34+ .Enrich .FromLogContext ()
35+ .WriteTo .Console ()
36+ .CreateLogger ();
37+
38+ // Other startup code
3939 ```
4040
4141** Finally , for .NET Core 2.0+**, in your `Startup` class's `Configure()` method , remove the existing logger configuration entries and
4242call `AddSerilog ()` on the provided `loggingBuilder `.
4343
4444```csharp
45- public void ConfigureServices (IServiceCollection services )
46- {
47- services .AddLogging (loggingBuilder =>
48- loggingBuilder .AddSerilog (dispose : true ));
45+ public void ConfigureServices (IServiceCollection services )
46+ {
47+ services .AddLogging (loggingBuilder =>
48+ loggingBuilder .AddSerilog (dispose : true ));
4949
50- // Other services ...
51- }
50+ // Other services ...
51+ }
5252```
5353
5454** For .NET Core 1 . 0 or 1 . 1 ** , in your `Startup ` class 's `Configure()` method, remove the existing logger configuration entries and call `AddSerilog()` on the provided `loggerFactory`.
5555
5656```
57- public void Configure (IApplicationBuilder app ,
58- IHostingEnvironment env ,
59- ILoggerFactory loggerfactory ,
60- IApplicationLifetime appLifetime )
61- {
62- loggerfactory .AddSerilog ();
63-
64- // Ensure any buffered events are sent at shutdown
65- appLifetime .ApplicationStopped .Register (Log .CloseAndFlush );
57+ public void Configure (IApplicationBuilder app ,
58+ IHostingEnvironment env ,
59+ ILoggerFactory loggerfactory ,
60+ IApplicationLifetime appLifetime )
61+ {
62+ loggerfactory .AddSerilog ();
63+
64+ // Ensure any buffered events are sent at shutdown
65+ appLifetime .ApplicationStopped .Register (Log .CloseAndFlush );
6666```
6767
6868That 's it! With the level bumped up a little you should see log output like:
@@ -87,10 +87,10 @@ _Serilog.Extensions.Logging_ captures the `ILogger`'s log category, but it's not
8787
8888To include the log category in the final written messages , add the `{SourceContext }` named hole to a customised `outputTemplate ` parameter value when configuring the relevant sink (s ). For example :
8989```csharp
90- .WriteTo .Console (
91- outputTemplate : " [{Timestamp:HH:mm:ss} {Level:u3}] {SourceContext}: {Message:lj}{NewLine}{Exception}" )
92- .WriteTo .File (" log.txt" ,
93- outputTemplate : " {Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {SourceContext}: {Message:lj}{NewLine}{Exception}" )
90+ .WriteTo .Console (
91+ outputTemplate : " [{Timestamp:HH:mm:ss} {Level:u3}] {SourceContext}: {Message:lj}{NewLine}{Exception}" )
92+ .WriteTo .File (" log.txt" ,
93+ outputTemplate : " {Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {SourceContext}: {Message:lj}{NewLine}{Exception}" )
9494```
9595
9696### Notes on Log Scopes
@@ -103,7 +103,8 @@ _Microsoft.Extensions.Logging_ provides the `BeginScope` API, which can be used
103103Using the extension method will add a `Scope ` property to your log events . This is most useful for adding simple "scope strings " to your events , as in the following code :
104104
105105```csharp
106- using (_logger .BeginScope (" Transaction" )) {
106+ using (_logger .BeginScope (" Transaction" ))
107+ {
107108 _logger .LogInformation (" Beginning..." );
108109 _logger .LogInformation (" Completed in {DurationMs}ms..." , 30 );
109110}
@@ -116,7 +117,8 @@ If you simply want to add a "bag" of additional properties to your log events, h
116117
117118```csharp
118119// WRONG! Prefer the dictionary or value tuple approach below instead
119- using (_logger .BeginScope (" TransactionId: {TransactionId}, ResponseJson: {ResponseJson}" , 12345 , jsonString )) {
120+ using (_logger .BeginScope (" TransactionId: {TransactionId}, ResponseJson: {ResponseJson}" , 12345 , jsonString ))
121+ {
120122 _logger .LogInformation (" Completed in {DurationMs}ms..." , 30 );
121123}
122124// Example JSON output:
@@ -138,11 +140,13 @@ Moreover, the template string within `BeginScope` is rather arbitrary when all y
138140A far better alternative is to use the `BeginScope <TState >(TState state )` method . If you provide any `IEnumerable <KeyValuePair <string , object >> ` to this method , then Serilog will output the key / value pairs as structured properties _without_ the `Scope ` property , as in this example :
139141
140142```csharp
141- var scopeProps = new Dictionary <string , object > {
143+ var scopeProps = new Dictionary <string , object >
144+ {
142145 { " TransactionId" , 12345 },
143146 { " ResponseJson" , jsonString },
144147};
145- using (_logger .BeginScope (scopeProps ) {
148+ using (_logger .BeginScope (scopeProps )
149+ {
146150 _logger .LogInformation (" Transaction completed in {DurationMs}ms..." , 30 );
147151}
148152// Example JSON output:
@@ -157,10 +161,12 @@ using (_logger.BeginScope(scopeProps) {
157161// }
158162 ```
159163
160- Alternatively provide a `ValueTuple < string , object ? > ` to this method , where `Item1 ` is the property name and `Item2 ` is the property value . Note that `T2 ` _must_ be `object ? `.
164+ Alternatively provide a `ValueTuple < string , object ? > ` to this method , where `Item1 ` is the property name and `Item2 ` is the property value .
165+ Note that `T2 ` _must_ be `object ? ` if your target platform is net462 or netstandard2 .0.
161166
162167```csharp
163- using (_logger .BeginScope ((" TransactionId" , (object ?)12345 )) {
168+ using (_logger .BeginScope ((" TransactionId" , 12345 ))
169+ {
164170 _logger .LogInformation (" Transaction completed in {DurationMs}ms..." , 30 );
165171}
166172// Example JSON output:
0 commit comments