Skip to content

Commit 2857be8

Browse files
svrooijsungam3r
andauthored
Docs: Added pushing properties to Readme (#217)
Docs: Added pushing properties to Readme Fixed #212 Co-authored-by: Ivan Maximov <[email protected]> Co-authored-by: Stephan van Rooij <[email protected]>
1 parent 6da3b1a commit 2857be8

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

README.md

+28
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,31 @@ The Azure Diagnostic Log Stream ships events from any files in the `D:\home\LogF
275275
flushToDiskInterval: TimeSpan.FromSeconds(1))
276276
.CreateLogger();
277277
```
278+
279+
### Pushing properties to the `ILogger<T>`
280+
281+
If you want to add extra properties to all logevents in a specific part of your code, you can add them to the **`ILogger<T>`** in **Microsoft.Extensions.Logging** with the following code. For this code to work, make sure you have added the `.Enrich.FromLogContext()` to the `.UseSerilog(...)` statement, as specified in the samples above.
282+
283+
```csharp
284+
// Microsoft.Extensions.Logging ILogger<T>
285+
// Yes, it's required to use a dictionary. See https://nblumhardt.com/2016/11/ilogger-beginscope/
286+
using (logger.BeginScope(new Dictionary<string, object>
287+
{
288+
["UserId"] = "svrooij",
289+
["OperationType"] = "update",
290+
}))
291+
{
292+
// UserId and OperationType are set for all logging events in these brackets
293+
}
294+
```
295+
296+
The code above results in the same outcome as if you would push properties in the **ILogger** in Serilog.
297+
298+
```csharp
299+
// Serilog ILogger
300+
using (logger.PushProperty("UserId", "svrooij"))
301+
using (logger.PushProperty("OperationType", "update"))
302+
{
303+
// UserId and OperationType are set for all logging events in these brackets
304+
}
305+
```

0 commit comments

Comments
 (0)