Skip to content

Commit d8637c2

Browse files
committed
Update readme with exaple of BeginScope with a ValueTuple.
1 parent 322c5f2 commit d8637c2

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ using (_logger.BeginScope("Transaction")) {
102102
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:
103103

104104
```csharp
105-
// WRONG! Prefer the dictionary approach below instead
105+
// WRONG! Prefer the dictionary or value tuple approach below instead
106106
using (_logger.BeginScope("TransactionId: {TransactionId}, ResponseJson: {ResponseJson}", 12345, jsonString)) {
107107
_logger.LogInformation("Completed in {DurationMs}ms...", 30);
108108
}
@@ -144,6 +144,23 @@ using (_logger.BeginScope(scopeProps) {
144144
// }
145145
```
146146

147+
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?`.
148+
149+
```csharp
150+
using (_logger.BeginScope(("TransactionId", (object?)12345)) {
151+
_logger.LogInformation("Transaction completed in {DurationMs}ms...", 30);
152+
}
153+
// Example JSON output:
154+
// {
155+
// "@t":"2020-10-29T19:05:56.4176816Z",
156+
// "@m":"Completed in 30ms...",
157+
// "@i":"51812baa",
158+
// "DurationMs":30,
159+
// "SourceContext":"SomeNamespace.SomeService",
160+
// "TransactionId": 12345
161+
// }
162+
```
163+
147164
### Versioning
148165

149166
This package tracks the versioning and target framework support of its [_Microsoft.Extensions.Logging_](https://nuget.org/packages/Microsoft.Extensions.Logging) dependency.

0 commit comments

Comments
 (0)