Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

Enrich Serilog events with properties from the current thread.

`WithThreadName()` is only supported in .NetStandard 2.0 and .NetFramework 4.5.

### Getting started

Install the package from NuGet:
Expand Down
1 change: 0 additions & 1 deletion global.json

This file was deleted.

5 changes: 2 additions & 3 deletions serilog-enrichers-thread.sln
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.329
# Visual Studio Version 17
VisualStudioVersion = 17.10.34928.147
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{037440DE-440B-4129-9F7A-09B42D00397E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{E9D1B5E1-DEB9-4A04-8BAB-24EC7240ADAF}"
ProjectSection(SolutionItems) = preProject
Build.ps1 = Build.ps1
global.json = global.json
README.md = README.md
assets\Serilog.snk = assets\Serilog.snk
EndProjectSection
Expand Down
6 changes: 3 additions & 3 deletions src/Serilog.Enrichers.Thread/Enrichers/ThreadIdEnricher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ namespace Serilog.Enrichers
/// <summary>
/// Enriches log events with a ThreadId property containing the <see cref="Environment.CurrentManagedThreadId"/>.
/// </summary>
public class ThreadIdEnricher : ILogEventEnricher
sealed class ThreadIdEnricher : ILogEventEnricher
{
/// <summary>
/// The property name added to enriched log events.
/// </summary>
public const string ThreadIdPropertyName = "ThreadId";
const string ThreadIdPropertyName = "ThreadId";

/// <summary>
/// The cached last created "ThreadId" property with some thread id. It is likely to be reused frequently so avoiding heap allocations.
Expand All @@ -43,7 +43,7 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
var threadId = Environment.CurrentManagedThreadId;

var last = _lastValue;
if (last == null || (int)((ScalarValue)last.Value).Value != threadId)
if (last is null || (int)((ScalarValue)last.Value).Value! != threadId)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the ! for these on the assumption that the ScalarValue is always created here, and doesn't have a null value

// no need to synchronize threads on write - just some of them will win
_lastValue = last = new LogEventProperty(ThreadIdPropertyName, new ScalarValue(threadId));

Expand Down
10 changes: 4 additions & 6 deletions src/Serilog.Enrichers.Thread/Enrichers/ThreadNameEnricher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if THREAD_NAME
using System.Threading;
using Serilog.Core;
using Serilog.Events;
Expand All @@ -22,12 +21,12 @@ namespace Serilog.Enrichers
/// <summary>
/// Enriches log events with a ThreadName property containing the <see cref="Thread.CurrentThread"/> <see cref="Thread.Name"/>.
/// </summary>
public class ThreadNameEnricher : ILogEventEnricher
sealed class ThreadNameEnricher : ILogEventEnricher
{
/// <summary>
/// The property name added to enriched log events.
/// </summary>
public const string ThreadNamePropertyName = "ThreadName";
const string ThreadNamePropertyName = "ThreadName";

/// <summary>
/// The cached last created "ThreadName" property with some thread name. It is likely to be reused frequently so avoiding heap allocations.
Expand All @@ -42,10 +41,10 @@ public class ThreadNameEnricher : ILogEventEnricher
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
var threadName = Thread.CurrentThread.Name;
if (threadName != null)
if (threadName is not null)
{
var last = _lastValue;
if (last == null || (string)((ScalarValue)last.Value).Value != threadName)
if (last is null || (string)((ScalarValue)last.Value).Value! != threadName)
// no need to synchronize threads on write - just some of them will win
_lastValue = last = new LogEventProperty(ThreadNamePropertyName, new ScalarValue(threadName));

Expand All @@ -54,4 +53,3 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
}
}
}
#endif
6 changes: 0 additions & 6 deletions src/Serilog.Enrichers.Thread/Properties/AssemblyInfo.cs

This file was deleted.

22 changes: 7 additions & 15 deletions src/Serilog.Enrichers.Thread/Serilog.Enrichers.Thread.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
<Description>Enrich Serilog events with properties from the current thread.</Description>
<VersionPrefix>3.2.0</VersionPrefix>
<Authors>Serilog Contributors</Authors>
<TargetFrameworks>net45;netstandard1.0;netstandard2.0</TargetFrameworks>
<!-- .NET Framework version targeting is frozen at these two TFMs. -->
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT'">net471;net462</TargetFrameworks>
<!-- Policy is to trim TFM-specific builds to `netstandard2.0`, `net6.0`,
all active LTS versions, and optionally the latest RTM version, when releasing new
major Serilog versions. -->
<TargetFrameworks>$(TargetFrameworks);net8.0;net6.0;netstandard2.0</TargetFrameworks>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>Serilog.Enrichers.Thread</AssemblyName>
Expand All @@ -22,12 +27,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="2.9.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
<PackageReference Include="Serilog" Version="4.0.0" />
</ItemGroup>

<ItemGroup>
Expand All @@ -37,12 +37,4 @@
</None>
</ItemGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net45' ">
<DefineConstants>$(DefineConstants);THREAD_NAME</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<DefineConstants>$(DefineConstants);THREAD_NAME</DefineConstants>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.


using System;
using System.Threading;
using Serilog.Configuration;
Expand All @@ -39,7 +38,6 @@ public static LoggerConfiguration WithThreadId(
return enrichmentConfiguration.With<ThreadIdEnricher>();
}

#if THREAD_NAME
/// <summary>
/// Enrich log events with a ThreadName property containing the <see cref="Thread.CurrentThread"/> <see cref="Thread.Name"/>.
/// </summary>
Expand All @@ -52,6 +50,5 @@ public static LoggerConfiguration WithThreadName(
if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration));
return enrichmentConfiguration.With<ThreadNameEnricher>();
}
#endif
}
}