Skip to content

Commit 17b7e55

Browse files
authored
Merge pull request #14 from StefanOssendorf/feature/readme-update
Readme updated to include WithThreadName [skip ci]
2 parents 088e235 + 7b72dc8 commit 17b7e55

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

README.md

Lines changed: 24 additions & 6 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,59 @@ 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.
21-
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.
25+
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.
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 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.
2832

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

3135
```csharp
36+
Thread.CurrentThread.Name = "MyWorker";
37+
3238
var logger = Log.Logger = new LoggerConfiguration()
3339
.MinimumLevel.Debug()
3440
.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}"))
41+
.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}"))
3642
.Enrich.WithThreadId()
3743
.CreateLogger();
3844
```
3945
Which would produce an output in the log file as follows:
4046
```
41-
2018-04-06 13:12:45.684 +02:00 [ERR] The file file_name.svg does not exist <4>
47+
2018-04-06 13:12:45.684 +02:00 [ERR] The file file_name.svg does not exist <4><MyWorker>
4248
```
43-
Where, <4> is an example thread id.
49+
Where, <4> is an example thread id and <MyWorker> is an example thread name.
4450

4551
To use the enricher, first install the NuGet package:
4652

4753
```powershell
4854
Install-Package Serilog.Enrichers.Thread
4955
```
5056

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

5270
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)