You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The ability to pass any `IEnumerable<KeyValuePair<string, object>>` to `ILogger.BeginScope` is an _extremely_ useful feature, but unfortunately it doesn't seem to be officially documented anywhere. I only discovered it while digging through this repo's source code! I assume this repo is the correct location for this information, as it only applies when one is using both Serilog and the _Microsoft.Extensions.Logging_ library, but I could see where it might be better off in the Serilog wiki. Looking forward to your feedback!
// {"@t":"2020-10-29T19:05:56.4176816Z","@m":"Completed in 30ms...","@i":"51812baa","DurationMs":30,"SourceContext":"SomeNamespace.SomeService","Scope":["Transaction"]}
100
+
```
101
+
102
+
Ifyousimplywanttoadda"bag"ofadditionalpropertiestoyourlogevents, however, thisextensionmethodapproachcanbeoverlyverbose. Forexample, toadd `TransactionId` and `ResponseJson` propertiestoyourlogevents, youwouldhavetodosomethinglikethefollowing:
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);
Notonlydoesthisaddtheunnecessary `Scope` propertytoyourevent, butitalsoduplicatesserializedvaluesbetween `Scope` andtheintendedproperties, asyoucanseeherewith `ResponseJson`. Ifthiswere"real"JSONlikeanAPIresponse, thenapotentiallyverylargeblockoftextwouldbeduplicatedwithinyourlogevent!Moreover, thetemplatestringwithin `BeginScope` isratherarbitrary when all you want to do is add a bag of properties, and you start mixing enriching concerns with formatting concerns.
0 commit comments