Skip to content

Commit 3930717

Browse files
authored
Merge pull request #182 from Rabadash8820/patch-1
Added notes about Log Scope usage to README
2 parents 7fa8d7c + 792e723 commit 3930717

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

README.md

+63
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,69 @@ That's it! With the level bumped up a little you should see log output like:
8080
[22:14:45.741 DBG] Handled. Status code: 304 File: /css/site.css
8181
```
8282

83+
### Notes on Log Scopes
84+
85+
_Microsoft.Extensions.Logging_ provides the `BeginScope` API, which can be used to add arbitrary properties to log events within a certain region of code. The API comes in two forms:
86+
87+
1. The method: `IDisposable BeginScope<TState>(TState state)`
88+
2. The extension method: `IDisposable BeginScope(this ILogger logger, string messageFormat, params object[] args)`
89+
90+
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:
91+
92+
```cs
93+
using (_logger.BeginScope("Transaction")) {
94+
_logger.LogInformation("Beginning...");
95+
_logger.LogInformation("Completed in {DurationMs}ms...", 30);
96+
}
97+
// Example JSON output:
98+
// {"@t":"2020-10-29T19:05:56.4126822Z","@m":"Beginning...","@i":"f6a328e9","SourceContext":"SomeNamespace.SomeService","Scope":["Transaction"]}
99+
// {"@t":"2020-10-29T19:05:56.4176816Z","@m":"Completed in 30ms...","@i":"51812baa","DurationMs":30,"SourceContext":"SomeNamespace.SomeService","Scope":["Transaction"]}
100+
```
101+
102+
If you simply want to add a "bag" of additional properties to your log events, however, this extension method approach can be overly verbose. For example, to add `TransactionId` and `ResponseJson` properties to your log events, you would have to do something like the following:
103+
104+
```cs
105+
// WRONG! Prefer the dictionary approach below instead
106+
using (_logger.BeginScope("TransactionId: {TransactionId}, ResponseJson: {ResponseJson}", 12345, jsonString)) {
107+
_logger.LogInformation("Completed in {DurationMs}ms...", 30);
108+
}
109+
// Example JSON output:
110+
// {
111+
// "@t":"2020-10-29T19:05:56.4176816Z",
112+
// "@m":"Completed in 30ms...",
113+
// "@i":"51812baa",
114+
// "DurationMs":30,
115+
// "SourceContext":"SomeNamespace.SomeService",
116+
// "TransactionId": 12345,
117+
// "ResponseJson": "{ \"Key1\": \"Value1\", \"Key2\": \"Value2\" }",
118+
// "Scope":["TransactionId: 12345, ResponseJson: { \"Key1\": \"Value1\", \"Key2\": \"Value2\" }"]
119+
// }
120+
```
121+
122+
Not only does this add the unnecessary `Scope` property to your event, but it also duplicates serialized values between `Scope` and the intended properties, as you can see here with `ResponseJson`. If this were "real" JSON like an API response, then a potentially very large block of text would be duplicated within your log event! Moreover, the template string within `BeginScope` is rather arbitrary when all you want to do is add a bag of properties, and you start mixing enriching concerns with formatting concerns.
123+
124+
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:
125+
126+
```cs
127+
var scopeProps = new Dictionary<string, object> {
128+
{ "TransactionId", 12345 },
129+
{ "ResponseJson", jsonString },
130+
};
131+
using (_logger.BeginScope(scopeProps) {
132+
_logger.LogInformation("Transaction completed in {DurationMs}ms...", 30);
133+
}
134+
// Example JSON output:
135+
// {
136+
// "@t":"2020-10-29T19:05:56.4176816Z",
137+
// "@m":"Completed in 30ms...",
138+
// "@i":"51812baa",
139+
// "DurationMs":30,
140+
// "SourceContext":"SomeNamespace.SomeService",
141+
// "TransactionId": 12345,
142+
// "ResponseJson": "{ \"Key1\": \"Value1\", \"Key2\": \"Value2\" }"
143+
// }
144+
```
145+
83146
### Credits
84147

85148
This package evolved from an earlier package _Microsoft.Framework.Logging.Serilog_ [provided by the ASP.NET team](https://github.com/aspnet/Logging/pull/182).

0 commit comments

Comments
 (0)