Skip to content

Commit cdce12d

Browse files
ThreadName Enricher added for Net45 and Standar2.0
ThreadName Enricher added for Net45 and NetStandard2.0. Unfortunately Thread is not available in NetStandard1.0. Changed the copyright statement to include 2019.
1 parent 396b40e commit cdce12d

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
16+
using System.Threading;
17+
using Serilog.Core;
18+
using Serilog.Events;
19+
20+
namespace Serilog.Enrichers {
21+
#if NET45 || NETSTANDARD2_0
22+
/// <summary>
23+
/// Enriches log events with a ThreadName property containing the
24+
/// </summary>
25+
public class ThreadNameEnricher : ILogEventEnricher
26+
{
27+
28+
/// <summary>
29+
/// The property name added to enriched log events.
30+
/// </summary>
31+
public const string ThreadNamePropertyName = "ThreadName";
32+
33+
34+
/// <summary>
35+
/// Enrich the log event.
36+
/// </summary>
37+
/// <param name="logEvent">The log event to enrich.</param>
38+
/// <param name="propertyFactory">Factory for creating new properties to add to the event.</param>
39+
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
40+
{
41+
logEvent.AddPropertyIfAbsent(new LogEventProperty(ThreadNamePropertyName, new ScalarValue(Thread.CurrentThread.Name)));
42+
}
43+
}
44+
#endif
45+
}

src/Serilog.Enrichers.Thread/ThreadLoggerConfigurationExtensions.cs

Lines changed: 15 additions & 1 deletion
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 NET45 || NETSTANDARD2_0
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
}
4256
}

0 commit comments

Comments
 (0)