From 71e875f408c47cd1d2b33badf963d7c12f21b4fa Mon Sep 17 00:00:00 2001 From: Stefan Ossendorf Date: Fri, 31 May 2019 16:46:11 +0200 Subject: [PATCH 1/2] WithThreadName() added to readme --- README.md | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index bd8781c..32eeed5 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # Serilog.Enrichers.Thread [![Build status](https://ci.appveyor.com/api/projects/status/2vgxdy3swg6eaj3f?svg=true)](https://ci.appveyor.com/project/serilog/serilog-enrichers-thread) [![NuGet Version](http://img.shields.io/nuget/v/Serilog.Enrichers.Thread.svg?style=flat)](https://www.nuget.org/packages/Serilog.Enrichers.Thread/) Enrich Serilog events with properties from the current thread. + +`WithThreadName()` is only supported in .NetStandard 2.0 and .NetFramework 4.5. ### Getting started @@ -10,13 +12,15 @@ Install the package from NuGet: Install-Package Serilog.Enrichers.Thread ``` -In your logger configuration, apply `Enrich.WithThreadId()`: +In your logger configuration, apply `Enrich.WithThreadId()` and `Enrich.WithThreadName()`: ```csharp Log.Logger = new LoggerConfiguration() .Enrich.WithThreadId() + .Enrich.WithThreadName() .CreateLogger(); ``` + Many sinks simply include all properties without further action required, so the thread id will be logged automatically. However, some sinks, such as the File and Console sinks use an output template and the new ThreadId may not be automatically output in your sink. In this case, in order for the ThreadId to show up in the logging, you will need to create or modify your output template. @@ -24,7 +28,7 @@ However, some sinks, such as the File and Console sinks use an output template a w.File(...., outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj} {Properties}{NewLine}{Exception}") ``` -Here, {Properties} can include not only ThreadId, but any other enrichment which is applied. Alternatively, {ThreadId} could be used instead, if you want to only add the thread id enrichment. +Here, {Properties} can include not only ThreadId, but any other enrichment which is applied. Alternatively, {ThreadId} could be used instead, if you want to only add the thread id enrichment or {ThreadName}, if you want to only add the thread name enrichment. An example, which also uses the Serilogs.Sinks.Async Nuget package, is below: @@ -32,15 +36,15 @@ An example, which also uses the Serilogs.Sinks.Async Nuget package, is below: var logger = Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .WriteTo.Console(restrictedToMinimumLevel:Serilog.Events.LogEventLevel.Information) - .WriteTo.Async(w=>w.File("..\\..\\..\\..\\logs\\SerilogLogFile.json", rollingInterval: RollingInterval.Day, outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj} <{ThreadId}>{NewLine}{Exception}")) + .WriteTo.Async(w=>w.File("..\\..\\..\\..\\logs\\SerilogLogFile.json", rollingInterval: RollingInterval.Day, outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj} <{ThreadId}><{ThreadName}>{NewLine}{Exception}")) .Enrich.WithThreadId() .CreateLogger(); ``` Which would produce an output in the log file as follows: ``` -2018-04-06 13:12:45.684 +02:00 [ERR] The file file_name.svg does not exist <4> +2018-04-06 13:12:45.684 +02:00 [ERR] The file file_name.svg does not exist <4> ``` -Where, <4> is an example thread id. +Where, <4> is an example thread id and is an example thread name. To use the enricher, first install the NuGet package: @@ -48,5 +52,17 @@ To use the enricher, first install the NuGet package: Install-Package Serilog.Enrichers.Thread ``` +Note: +The {ThreadName} property will only be attached when it is not null. Otherwise it will be omitted. +If you want to get this property always attached you can use the following: +```csharp +using Serilog.Enrichers; + +Log.Logger = new LoggerConfiguration() + .Enrich.WithThreadName() + .Enrich.WithProperty(ThreadNameEnricher.ThreadNamePropertyName, "MyDefault") + .CreateLogger(); +``` +The enrichment order is important. Otherwise "MyDefault" would always win. Copyright © 2016 Serilog Contributors - Provided under the [Apache License, Version 2.0](http://apache.org/licenses/LICENSE-2.0.html). From 7b72dc866fb7954ca239d4c584c60136d850f6db Mon Sep 17 00:00:00 2001 From: Stefan Ossendorf Date: Fri, 31 May 2019 16:51:22 +0200 Subject: [PATCH 2/2] Example completed Readme added & example fixed to reflect out of the box usage. --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 32eeed5..d4e0d26 100644 --- a/README.md +++ b/README.md @@ -22,17 +22,19 @@ Log.Logger = new LoggerConfiguration() ``` Many sinks simply include all properties without further action required, so the thread id will be logged automatically. -However, some sinks, such as the File and Console sinks use an output template and the new ThreadId may not be automatically output in your sink. In this case, in order for the ThreadId to show up in the logging, you will need to create or modify your output template. +However, some sinks, such as the File and Console sinks use an output template and the new ThreadId may not be automatically output in your sink. In this case, in order for the ThreadId or ThreadName to show up in the logging, you will need to create or modify your output template. ```csharp w.File(...., outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj} {Properties}{NewLine}{Exception}") ``` -Here, {Properties} can include not only ThreadId, but any other enrichment which is applied. Alternatively, {ThreadId} could be used instead, if you want to only add the thread id enrichment or {ThreadName}, if you want to only add the thread name enrichment. +Here, {Properties} can include not only ThreadId and ThreadName, but any other enrichment which is applied. Alternatively, {ThreadId} could be used instead, if you want to only add the thread id enrichment and {ThreadName}, if you want to only add the thread name enrichment. An example, which also uses the Serilogs.Sinks.Async Nuget package, is below: ```csharp + Thread.CurrentThread.Name = "MyWorker"; + var logger = Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .WriteTo.Console(restrictedToMinimumLevel:Serilog.Events.LogEventLevel.Information)