@@ -28,41 +28,41 @@ using Serilog;
28
28
29
29
public class Startup
30
30
{
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
39
39
```
40
40
41
41
** Finally , for .NET Core 2.0+**, in your `Startup` class's `Configure()` method , remove the existing logger configuration entries and
42
42
call `AddSerilog ()` on the provided `loggingBuilder `.
43
43
44
44
```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 ));
49
49
50
- // Other services ...
51
- }
50
+ // Other services ...
51
+ }
52
52
```
53
53
54
54
** 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`.
55
55
56
56
```
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 );
66
66
```
67
67
68
68
That '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
87
87
88
88
To 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 :
89
89
```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}" )
94
94
```
95
95
96
96
### Notes on Log Scopes
@@ -103,7 +103,8 @@ _Microsoft.Extensions.Logging_ provides the `BeginScope` API, which can be used
103
103
Using 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 :
104
104
105
105
```csharp
106
- using (_logger .BeginScope (" Transaction" )) {
106
+ using (_logger .BeginScope (" Transaction" ))
107
+ {
107
108
_logger .LogInformation (" Beginning..." );
108
109
_logger .LogInformation (" Completed in {DurationMs}ms..." , 30 );
109
110
}
@@ -116,7 +117,8 @@ If you simply want to add a "bag" of additional properties to your log events, h
116
117
117
118
```csharp
118
119
// 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
+ {
120
122
_logger .LogInformation (" Completed in {DurationMs}ms..." , 30 );
121
123
}
122
124
// Example JSON output:
@@ -138,11 +140,13 @@ Moreover, the template string within `BeginScope` is rather arbitrary when all y
138
140
A 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 :
139
141
140
142
```csharp
141
- var scopeProps = new Dictionary <string , object > {
143
+ var scopeProps = new Dictionary <string , object >
144
+ {
142
145
{ " TransactionId" , 12345 },
143
146
{ " ResponseJson" , jsonString },
144
147
};
145
- using (_logger .BeginScope (scopeProps ) {
148
+ using (_logger .BeginScope (scopeProps )
149
+ {
146
150
_logger .LogInformation (" Transaction completed in {DurationMs}ms..." , 30 );
147
151
}
148
152
// Example JSON output:
@@ -157,10 +161,12 @@ using (_logger.BeginScope(scopeProps) {
157
161
// }
158
162
```
159
163
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.
161
166
162
167
```csharp
163
- using (_logger .BeginScope ((" TransactionId" , (object ?)12345 )) {
168
+ using (_logger .BeginScope ((" TransactionId" , 12345 ))
169
+ {
164
170
_logger .LogInformation (" Transaction completed in {DurationMs}ms..." , 30 );
165
171
}
166
172
// Example JSON output:
0 commit comments