Skip to content

Commit 3839430

Browse files
authored
Merge pull request #11 from StefanOssendorf/threadName-enricher
ThreadName Enricher added for Net45 and Standard2.0
2 parents 396b40e + 0b961db commit 3839430

File tree

4 files changed

+73
-3
lines changed

4 files changed

+73
-3
lines changed

src/Serilog.Enrichers.Thread/Enrichers/ThreadIdEnricher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013-2015 Serilog Contributors
1+
// Copyright 2013-2019 Serilog Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2013-2019 Serilog Contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#if THREAD_NAME
16+
using System.Threading;
17+
using Serilog.Core;
18+
using Serilog.Events;
19+
20+
namespace Serilog.Enrichers
21+
{
22+
23+
/// <summary>
24+
/// Enriches log events with a ThreadName property containing the
25+
/// </summary>
26+
public class ThreadNameEnricher : ILogEventEnricher
27+
{
28+
/// <summary>
29+
/// The property name added to enriched log events.
30+
/// </summary>
31+
public const string ThreadNamePropertyName = "ThreadName";
32+
33+
/// <summary>
34+
/// Enrich the log event.
35+
/// </summary>
36+
/// <param name="logEvent">The log event to enrich.</param>
37+
/// <param name="propertyFactory">Factory for creating new properties to add to the event.</param>
38+
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
39+
{
40+
var threadName = Thread.CurrentThread.Name;
41+
if (threadName != null)
42+
{
43+
logEvent.AddPropertyIfAbsent(new LogEventProperty(ThreadNamePropertyName, new ScalarValue(threadName)));
44+
}
45+
}
46+
}
47+
}
48+
#endif

src/Serilog.Enrichers.Thread/Serilog.Enrichers.Thread.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,12 @@
2828
<Reference Include="Microsoft.CSharp" />
2929
</ItemGroup>
3030

31+
<PropertyGroup Condition=" '$(TargetFramework)' == 'net45' ">
32+
<DefineConstants>$(DefineConstants);THREAD_NAME</DefineConstants>
33+
</PropertyGroup>
34+
35+
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
36+
<DefineConstants>$(DefineConstants);THREAD_NAME</DefineConstants>
37+
</PropertyGroup>
38+
3139
</Project>

src/Serilog.Enrichers.Thread/ThreadLoggerConfigurationExtensions.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013-2016 Serilog Contributors
1+
// Copyright 2013-2019 Serilog Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -38,5 +38,19 @@ public static LoggerConfiguration WithThreadId(
3838
if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration));
3939
return enrichmentConfiguration.With<ThreadIdEnricher>();
4040
}
41+
42+
#if THREAD_NAME
43+
/// <summary>
44+
/// Enrich log events with a ThreadName property containing the <see cref="Thread.CurrentThread"/> <see cref="Thread.Name"/>.
45+
/// </summary>
46+
/// <param name="enrichmentConfiguration"></param>
47+
/// <returns></returns>
48+
public static LoggerConfiguration WithThreadName(
49+
this LoggerEnrichmentConfiguration enrichmentConfiguration)
50+
{
51+
if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration));
52+
return enrichmentConfiguration.With<ThreadNameEnricher>();
53+
}
54+
#endif
4155
}
42-
}
56+
}

0 commit comments

Comments
 (0)