|
| 1 | +| README.md | |
| 2 | +|:---| |
| 3 | + |
| 4 | +<div align="center"> |
| 5 | + |
| 6 | +<img src="asset/serilog-enrichers-globallogcontext-nuget.png" alt="Serilog.Enrichers.GlobalLogContext" width="100" /> |
| 7 | + |
| 8 | +</div> |
| 9 | + |
| 10 | +<h1 align="center">Serilog.Enrichers.GlobalLogContext</h1> |
| 11 | +<div align="center"> |
| 12 | + |
| 13 | +A [Serilog](https://serilog.net) enricher to dynamically add properties to all log events of your application. |
| 14 | + |
| 15 | +[](https://www.nuget.org/packages/Serilog.Enrichers.GlobalLogContext/) [](http://stackoverflow.com/questions/tagged/serilog) |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +</div> |
| 20 | + |
| 21 | +## Give a Star! :star: |
| 22 | + |
| 23 | +If you like or are using this project please give it a star. Thanks! |
| 24 | + |
| 25 | +## Getting started :rocket: |
| 26 | + |
| 27 | +Install the [Serilog.Enrichers.GlobalLogContext](https://www.nuget.org/packages/Serilog.Enrichers.GlobalLogContext/) package from NuGet: |
| 28 | + |
| 29 | +```powershell |
| 30 | +Install-Package Serilog.Enrichers.GlobalLogContext |
| 31 | +``` |
| 32 | + |
| 33 | +Add the Serilog namespaces to your C# file: |
| 34 | + |
| 35 | +```csharp |
| 36 | +using Serilog; |
| 37 | +using Serilog.Context; |
| 38 | +``` |
| 39 | + |
| 40 | +Include the global log context enricher in your logger configuration: |
| 41 | + |
| 42 | +```csharp |
| 43 | +Log.Logger = new LoggerConfiguration() |
| 44 | + .Enrich.FromGlobalLogContext() |
| 45 | + // ... other configuration ... |
| 46 | + .CreateLogger(); |
| 47 | +``` |
| 48 | + |
| 49 | +The `FromGlobalLogContext()` enricher dynamically adds properties present in the `Serilog.Context.GlobalLogContext`, to _all_ produced events in your application. |
| 50 | + |
| 51 | +Then, properties can be added and removed from the global log context using `GlobalLogContext.PushProperty()`: |
| 52 | + |
| 53 | +```csharp |
| 54 | +GlobalLogContext.PushProperty("AppVersion", GetThisAppVersion()); |
| 55 | +GlobalLogContext.PushProperty("OperatingSystem", GetCurrentOS()); |
| 56 | +``` |
| 57 | + |
| 58 | +After the code above is executed, any log event written to any Serilog sink will carry the properties `AppVersion` and `OperatingSystem` automatically. |
| 59 | + |
| 60 | +### Replacing and removing properties |
| 61 | + |
| 62 | +When properties are added to the global log context, a disposable object is returned, which can be used to remove the property from the global stack at a later time: |
| 63 | + |
| 64 | +```csharp |
| 65 | +GlobalLogContext.PushProperty("A", 1); |
| 66 | + |
| 67 | +Log.Information("Carries property A = 1"); |
| 68 | + |
| 69 | +using (GlobalLogContext.PushProperty("A", 2)) |
| 70 | +using (GlobalLogContext.PushProperty("B", 1)) |
| 71 | +{ |
| 72 | + Log.Information("Carries A = 2 and B = 1"); |
| 73 | +} |
| 74 | + |
| 75 | +Log.Information("Carries property A = 1, again"); |
| 76 | +``` |
| 77 | + |
| 78 | +Pushing a property onto the global log context will override any existing properties with the same name, until the object returned from `PushProperty()` is disposed, as the property `A` in the example demonstrates. |
| 79 | + |
| 80 | +**Important:** properties _must_ be popped from the global log context in the precise order in which they were added. Behavior otherwise is undefined. |
| 81 | + |
| 82 | +### Multi-threading |
| 83 | + |
| 84 | +By design, for performance reasons, adding or removing properties in the global log context are *not* thread-safe operations. If you have multiple threads modifying the `GlobalLogContext` at the same time, you should call `Lock` or `LockAsync` before making changes to the `GlobalLogContext`. e.g.: |
| 85 | + |
| 86 | +```csharp |
| 87 | +// sync |
| 88 | +using (GlobalLogContext.Lock()) |
| 89 | +{ |
| 90 | + GlobalLogContext.PushProperty("A", 3); |
| 91 | +} |
| 92 | + |
| 93 | +// async |
| 94 | +using (await GlobalLogContext.LockAsync()) |
| 95 | +{ |
| 96 | + GlobalLogContext.PushProperty("A", 3); |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +### Example of a .NET app using GlobalLogContext |
| 101 | + |
| 102 | +In the [sample](sample/) folder, there's an example of a .NET app that uses Serilog for logging to the `Console` using this enricher, as described above. |
| 103 | + |
| 104 | +## Why not just use `.Enrich.With` or `.Enrich.WithProperty`? |
| 105 | + |
| 106 | +When setting up the Serilog logging pipeline, you have the option to include properties in all log events using a native feature of Serilog: |
| 107 | + |
| 108 | +```csharp |
| 109 | +Log.Logger = new LoggerConfiguration() |
| 110 | + .Enrich.WithProperty("AppVersion", GetThisAppVersion()) |
| 111 | + .Enrich.WithProperty("OperatingSystem", GetCurrentOS()) |
| 112 | + // ... other configuration ... |
| 113 | + .CreateLogger(); |
| 114 | +``` |
| 115 | + |
| 116 | +The equivalent using `Serilog.Enrichers.GlobalLogContext` would be: |
| 117 | + |
| 118 | +```csharp |
| 119 | +Log.Logger = new LoggerConfiguration() |
| 120 | + .Enrich.FromGlobalLogContext() |
| 121 | + // ... other configuration ... |
| 122 | + .CreateLogger(); |
| 123 | + |
| 124 | +// ... other code ... |
| 125 | +
|
| 126 | +GlobalLogContext.PushProperty("AppVersion", GetThisAppVersion()); |
| 127 | +GlobalLogContext.PushProperty("OperatingSystem", GetCurrentOS()); |
| 128 | +``` |
| 129 | + |
| 130 | +As you can see, the main difference is that `Serilog.Enrichers.GlobalLogContext` allows you to add properties at a later time, long _after_ your Serilog logging pipeline has been configured, which can be very useful in scenarios where the properties being added are not immediately available during the bootstrapping of the app. In addition, properties in the `GlobalLogContext` can be replaced or removed at any time during the execution of the app. |
| 131 | + |
| 132 | +## Release History |
| 133 | + |
| 134 | +Click on the [Releases](https://github.com/augustoproiete/Serilog.Enrichers.GlobalLogContext/releases) tab on GitHub. |
| 135 | + |
| 136 | +--- |
| 137 | + |
| 138 | +_Copyright © 2021 C. Augusto Proiete & Contributors - Provided under the [Apache License, Version 2.0](LICENSE)._ |
0 commit comments