Skip to content

Commit 71e875f

Browse files
WithThreadName() added to readme
1 parent 088e235 commit 71e875f

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

README.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# 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/)
22

33
Enrich Serilog events with properties from the current thread.
4+
5+
`WithThreadName()` is only supported in .NetStandard 2.0 and .NetFramework 4.5.
46

57
### Getting started
68

@@ -10,43 +12,57 @@ Install the package from NuGet:
1012
Install-Package Serilog.Enrichers.Thread
1113
```
1214

13-
In your logger configuration, apply `Enrich.WithThreadId()`:
15+
In your logger configuration, apply `Enrich.WithThreadId()` and `Enrich.WithThreadName()`:
1416

1517
```csharp
1618
Log.Logger = new LoggerConfiguration()
1719
.Enrich.WithThreadId()
20+
.Enrich.WithThreadName()
1821
.CreateLogger();
1922
```
23+
2024
Many sinks simply include all properties without further action required, so the thread id will be logged automatically.
2125
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.
2226

2327
```csharp
2428
w.File(...., outputTemplate:
2529
"{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj} {Properties}{NewLine}{Exception}")
2630
```
27-
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.
31+
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.
2832

2933
An example, which also uses the Serilogs.Sinks.Async Nuget package, is below:
3034

3135
```csharp
3236
var logger = Log.Logger = new LoggerConfiguration()
3337
.MinimumLevel.Debug()
3438
.WriteTo.Console(restrictedToMinimumLevel:Serilog.Events.LogEventLevel.Information)
35-
.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}"))
39+
.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}"))
3640
.Enrich.WithThreadId()
3741
.CreateLogger();
3842
```
3943
Which would produce an output in the log file as follows:
4044
```
41-
2018-04-06 13:12:45.684 +02:00 [ERR] The file file_name.svg does not exist <4>
45+
2018-04-06 13:12:45.684 +02:00 [ERR] The file file_name.svg does not exist <4><MyWorker>
4246
```
43-
Where, <4> is an example thread id.
47+
Where, <4> is an example thread id and <MyWorker> is an example thread name.
4448

4549
To use the enricher, first install the NuGet package:
4650

4751
```powershell
4852
Install-Package Serilog.Enrichers.Thread
4953
```
5054

55+
Note:
56+
The {ThreadName} property will only be attached when it is not null. Otherwise it will be omitted.
57+
If you want to get this property always attached you can use the following:
58+
```csharp
59+
using Serilog.Enrichers;
60+
61+
Log.Logger = new LoggerConfiguration()
62+
.Enrich.WithThreadName()
63+
.Enrich.WithProperty(ThreadNameEnricher.ThreadNamePropertyName, "MyDefault")
64+
.CreateLogger();
65+
```
66+
The enrichment order is important. Otherwise "MyDefault" would always win.
5167

5268
Copyright &copy; 2016 Serilog Contributors - Provided under the [Apache License, Version 2.0](http://apache.org/licenses/LICENSE-2.0.html).

0 commit comments

Comments
 (0)