Serilog.Enrichers.Thread 4.0.0-dev-00762

Serilog.Enrichers.Thread Build status NuGet Version

Enrich Serilog events with properties from the current thread.

Getting started

Install the package from NuGet:

Install-Package Serilog.Enrichers.Thread

In your logger configuration, apply Enrich.WithThreadId() and Enrich.WithThreadName():

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 or ThreadName to show up in the logging, you will need to create or modify your output template.

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 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:

            Thread.CurrentThread.Name = "MyWorker";
              
            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}><{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><MyWorker>

Where, <4> is an example thread id and <MyWorker> is an example thread name.

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:

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.

Showing the top 20 packages that depend on Serilog.Enrichers.Thread.

Packages Downloads
Alpata.Helper
Option yönetimi için generik eklentiler sağlandı
6
Alpata.Helper
Option yönetimi için generik eklentiler sağlandı
10
Alpata.Helper
Option yönetimi için generik eklentiler sağlandı
11
Alpata.Helper
Option yönetimi için generik eklentiler sağlandı
12
Alpata.Helper
Option yönetimi için generik eklentiler sağlandı
13
Alpata.Helper
Option yönetimi için generik eklentiler sağlandı
14
Alpata.Helper
Option yönetimi için generik eklentiler sağlandı
15
Alpata.Helper
Option yönetimi için generik eklentiler sağlandı
18
Alpata.Helper
Option yönetimi için generik eklentiler sağlandı
23
Alpata.Helper
Option yönetimi için generik eklentiler sağlandı
24
Alpata.Helper
Option yönetimi için generik eklentiler sağlandı
25
Alpata.Helper
Option yönetimi için generik eklentiler sağlandı
26
Alpata.Helper
Option yönetimi için generik eklentiler sağlandı
35
Alpata.Helper
Option yönetimi için generik eklentiler sağlandı
142
Alpata.Helper
Option yönetimi için generik eklentiler sağlandı
279

.NET Framework 4.6.2

.NET Framework 4.7.1

.NET 6.0

.NET 8.0

.NET Standard 2.0

Version Downloads Last updated
4.0.0 158 04/06/2025
4.0.0-dev-00770 15 04/29/2025
4.0.0-dev-00767 14 05/03/2025
4.0.0-dev-00762 16 04/29/2025
3.2.0-dev-00756 15 04/29/2025
3.2.0-dev-00752 17 04/29/2025
3.2.0-dev-00750 15 04/29/2025
3.2.0-dev-00747 16 04/29/2025
3.1.1-dev-00745 16 04/29/2025
3.1.1-dev-00743 13 04/29/2025
3.1.0 19 04/29/2025
3.1.0-dev-00739 17 04/29/2025
3.0.1-dev-00737 15 04/29/2025
3.0.1-dev-00731 14 05/03/2025
3.0.1-dev-00728 17 04/29/2025
3.0.1-dev-00727 17 04/29/2025
3.0.0 19 04/07/2025
3.0.0-dev-00721 18 04/28/2025
2.0.1-dev-00720 15 04/28/2025
2.0.0 20 05/25/2025
2.0.0-rc-712 14 04/29/2025
2.0.0-beta-705 19 04/29/2025
2.0.0-beta-701 19 04/29/2025
2.0.0-beta-700 16 04/29/2025
2.0.0-beta-521 18 04/29/2025
2.0.0-beta-519 18 04/29/2025
2.0.0-beta-516 19 04/29/2025
2.0.0-beta-515 19 04/29/2025
2.0.0-beta-513 19 04/29/2025
2.0.0-beta-511 17 05/04/2025
2.0.0-beta-509 18 04/29/2025
2.0.0-beta-507 19 04/29/2025
2.0.0-beta-505 17 04/29/2025
2.0.0-beta-502 19 04/29/2025
2.0.0-beta-499 20 04/29/2025