Skip to content

Updates for Serilog 4 #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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: 1 addition & 1 deletion Build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ foreach ($src in ls src/*) {

echo "build: Packaging project in $src"

& dotnet pack -c Release -o ..\..\artifacts --version-suffix=$suffix
& dotnet pack -c Release -o ..\..\artifacts --version-suffix=$suffix -p:ContinuousIntegrationBuild=true
if($LASTEXITCODE -ne 0) { exit 1 }

Pop-Location
Expand Down
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
5 changes: 4 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ build_script:
test: off
artifacts:
- path: artifacts/Serilog.*.nupkg
- path: artifacts/Serilog.*.snupkg
deploy:
- provider: NuGet
api_key:
Expand All @@ -17,7 +18,9 @@ deploy:
- provider: GitHub
auth_token:
secure: p4LpVhBKxGS5WqucHxFQ5c7C8cP74kbNB0Z8k9Oxx/PMaDQ1+ibmoexNqVU5ZlmX
artifact: /Serilog.*\.nupkg/
artifacts:
/Serilog.*\.nupkg/
/Serilog.*\.snupkg/
tag: v$(appveyor_build_version)
on:
branch: master
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.

32 changes: 15 additions & 17 deletions src/Serilog.Enrichers.Thread/Serilog.Enrichers.Thread.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

<PropertyGroup>
<Description>Enrich Serilog events with properties from the current thread.</Description>
<VersionPrefix>3.2.0</VersionPrefix>
<VersionPrefix>4.0.0</VersionPrefix>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<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 @@ -16,33 +22,25 @@
<PackageIcon>serilog-enricher-nuget.png</PackageIcon>
<PackageProjectUrl>http://serilog.net</PackageProjectUrl>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
</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>
<None Include="..\..\assets\serilog-enricher-nuget.png">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
<None Include="../../README.md" Pack="true" Visible="false" PackagePath="/" />
</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
}
}