Skip to content

8.0.0 Release #235

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 10 commits into from
Nov 14, 2023
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
8 changes: 5 additions & 3 deletions Build.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
echo "build: Build started"

$env:Path = "$pwd/.dotnetcli;$env:Path"

Push-Location $PSScriptRoot

if(Test-Path .\artifacts) {
Expand All @@ -26,7 +28,7 @@ foreach ($src in ls src/*) {
& dotnet pack -c Release --include-source -o ..\..\artifacts
}

if($LASTEXITCODE -ne 0) { exit 1 }
if($LASTEXITCODE -ne 0) { throw "build failed" }

Pop-Location
}
Expand All @@ -37,7 +39,7 @@ foreach ($test in ls test/*.PerformanceTests) {
echo "build: Building performance test project in $test"

& dotnet build -c Release
if($LASTEXITCODE -ne 0) { exit 2 }
if($LASTEXITCODE -ne 0) { throw "test failed" }

Pop-Location
}
Expand All @@ -48,7 +50,7 @@ foreach ($test in ls test/*.Tests) {
echo "build: Testing project in $test"

& dotnet test -c Release
if($LASTEXITCODE -ne 0) { exit 3 }
if($LASTEXITCODE -ne 0) { throw "test failed" }

Pop-Location
}
Expand Down
9 changes: 9 additions & 0 deletions Setup.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
$ErrorActionPreference = "Stop"

$RequiredDotnetVersion = $(cat ./global.json | convertfrom-json).sdk.version

New-Item -ItemType Directory -Force "./build/" | Out-Null

Invoke-WebRequest "https://dot.net/v1/dotnet-install.ps1" -OutFile "./build/installcli.ps1"
& ./build/installcli.ps1 -InstallDir "$pwd/.dotnetcli" -NoPath -Version $RequiredDotnetVersion
if ($LASTEXITCODE) { throw ".NET install failed" }
5 changes: 3 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ version: '{build}'
skip_tags: true
image: Visual Studio 2022
install:
- ps: mkdir -Force ".\build\" | Out-Null
- pwsh: ./Setup.ps1
- pwsh: mkdir -Force ".\build\" | Out-Null
build_script:
- ps: ./Build.ps1
- pwsh: ./Build.ps1
test: off
artifacts:
- path: artifacts/Serilog.*.nupkg
Expand Down
5 changes: 5 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"sdk": {
"version": "8.0.100"
}
}
8 changes: 4 additions & 4 deletions samples/Sample/Sample.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<AssemblyName>Sample</AssemblyName>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
Expand All @@ -12,9 +12,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.0" />
</ItemGroup>

</Project>
3 changes: 3 additions & 0 deletions serilog-extensions-logging.sln
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{9C21B9
Directory.Build.targets = Directory.Build.targets
README.md = README.md
assets\Serilog.snk = assets\Serilog.snk
build.sh = build.sh
Setup.ps1 = Setup.ps1
global.json = global.json
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Extensions.Logging.Benchmarks", "test\Serilog.Extensions.Logging.Benchmarks\Serilog.Extensions.Logging.Benchmarks.csproj", "{6D5986FF-EECD-4E75-8BC6-A5F78AB549B2}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Reflection;
using Serilog.Debugging;
using System.Collections.Concurrent;
using System.Diagnostics;

namespace Serilog.Extensions.Logging;

Expand Down Expand Up @@ -156,8 +157,12 @@ LogEvent PrepareWrite<TState>(LogEventLevel level, EventId eventId, TState state
if (eventId.Id != 0 || eventId.Name != null)
properties.Add(CreateEventIdProperty(eventId));

var (traceId, spanId) = Activity.Current is { } activity ?
(activity.TraceId, activity.SpanId) :
(default(ActivityTraceId), default(ActivitySpanId));

var parsedTemplate = MessageTemplateParser.Parse(messageTemplate ?? "");
return new LogEvent(DateTimeOffset.Now, level, exception, parsedTemplate, properties);
return new LogEvent(DateTimeOffset.Now, level, exception, parsedTemplate, properties, traceId, spanId);
}

static object? AsLoggableValue<TState>(TState state, Func<TState, Exception?, string>? formatter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
<PropertyGroup>
<Description>Low-level Serilog provider for Microsoft.Extensions.Logging</Description>
<!-- This must match the major and minor components of the referenced Microsoft.Extensions.Logging package. -->
<VersionPrefix>7.0.0</VersionPrefix>
<VersionPrefix>8.0.0</VersionPrefix>
<Authors>Microsoft;Serilog Contributors</Authors>
<!-- These must match the Dependencies tab in https://www.nuget.org/packages/microsoft.extensions.logging at
the target version. -->
<TargetFrameworks>net462;netstandard2.0;netstandard2.1;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net462;netstandard2.0;netstandard2.1;net6.0;net7.0;net8.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>serilog;Microsoft.Extensions.Logging</PackageTags>
<PackageIcon>serilog-extension-nuget.png</PackageIcon>
Expand All @@ -29,9 +29,9 @@
<None Include="..\..\assets\serilog-extension-nuget.png" Pack="true" PackagePath="" Visible="false" />
<None Include="..\..\README.md" Pack="true" PackagePath="\" Visible="false" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Serilog" Version="2.12.0" />
<PackageReference Include="Serilog" Version="3.1.1" />
<!-- The version of this reference must match the major and minor components of the package version prefix. -->
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Nullable" Version="1.3.1" PrivateAssets="All" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

Expand All @@ -10,10 +10,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" PrivateAssets="All" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" PrivateAssets="All" />
<PackageReference Include="xunit" Version="2.6.1" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.10" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net7.0;net472</TargetFrameworks>
<TargetFrameworks>net8.0;net48</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net48' ">
<DefineConstants>$(DefineConstants);FORCE_W3C_ACTIVITY_ID</DefineConstants>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Serilog.Extensions.Logging\Serilog.Extensions.Logging.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" PrivateAssets="all" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="Shouldly" Version="4.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" PrivateAssets="all" />
<PackageReference Include="xunit" Version="2.6.1" />
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="PublicApiGenerator" Version="11.0.0" />
</ItemGroup>

Expand Down
47 changes: 39 additions & 8 deletions test/Serilog.Extensions.Logging.Tests/SerilogLoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections;
using System.Diagnostics;
using Serilog.Events;
using Microsoft.Extensions.Logging;
using Serilog.Debugging;
Expand Down Expand Up @@ -138,11 +139,11 @@ public void LogsCorrectMessage()

logger.Log<object>(LogLevel.Information, 0, null!, null!, null!);
logger.Log(LogLevel.Information, 0, TestMessage, null!, null!);
logger.Log<object>(LogLevel.Information, 0, null!, null!, (_, __) => TestMessage);
logger.Log<object>(LogLevel.Information, 0, null!, null!, (_, _) => TestMessage);

Assert.Equal(3, sink.Writes.Count);

Assert.Equal(1, sink.Writes[0].Properties.Count);
Assert.Single(sink.Writes[0].Properties);
Assert.Empty(sink.Writes[0].RenderMessage());

Assert.Equal(2, sink.Writes[1].Properties.Count);
Expand Down Expand Up @@ -308,7 +309,7 @@ public void WhenDisposeIsTrueProvidedLoggerIsDisposed()
}

[Fact]
public void BeginScopeDestructuresObjectsWhenDestructurerIsUsedInMessageTemplate()
public void BeginScopeDestructuresObjectsWhenCapturingOperatorIsUsedInMessageTemplate()
{
var (logger, sink) = SetUp(LogLevel.Trace);

Expand All @@ -328,7 +329,7 @@ public void BeginScopeDestructuresObjectsWhenDestructurerIsUsedInMessageTemplate
}

[Fact]
public void BeginScopeDestructuresObjectsWhenDestructurerIsUsedInDictionary()
public void BeginScopeDestructuresObjectsWhenCapturingOperatorIsUsedInDictionary()
{
var (logger, sink) = SetUp(LogLevel.Trace);

Expand All @@ -348,7 +349,7 @@ public void BeginScopeDestructuresObjectsWhenDestructurerIsUsedInDictionary()
}

[Fact]
public void BeginScopeDoesNotModifyKeyWhenDestructurerIsNotUsedInMessageTemplate()
public void BeginScopeDoesNotModifyKeyWhenCapturingOperatorIsNotUsedInMessageTemplate()
{
var (logger, sink) = SetUp(LogLevel.Trace);

Expand All @@ -362,7 +363,7 @@ public void BeginScopeDoesNotModifyKeyWhenDestructurerIsNotUsedInMessageTemplate
}

[Fact]
public void BeginScopeDoesNotModifyKeyWhenDestructurerIsNotUsedInDictionary()
public void BeginScopeDoesNotModifyKeyWhenCapturingOperatorIsNotUsedInDictionary()
{
var (logger, sink) = SetUp(LogLevel.Trace);

Expand Down Expand Up @@ -466,7 +467,10 @@ public void MismatchedMessageTemplateParameterCountIsHandled()
{
var (logger, sink) = SetUp(LogLevel.Trace);

#pragma warning disable CA2017
// ReSharper disable once StructuredMessageTemplateProblem
logger.LogInformation("Some test message with {Two} {Properties}", "OneProperty");
#pragma warning restore CA2017

Assert.Empty(sink.Writes);
}
Expand All @@ -475,7 +479,7 @@ public void MismatchedMessageTemplateParameterCountIsHandled()
public void ExceptionFromAuditSinkIsUnhandled()
{
var serilogLogger = new LoggerConfiguration()
.AuditTo.Sink(new MySink())
.AuditTo.Sink(new UnimplementedSink())
.CreateLogger();

var provider = new SerilogLoggerProvider(serilogLogger);
Expand All @@ -486,11 +490,38 @@ public void ExceptionFromAuditSinkIsUnhandled()
Assert.Equal("Oops", ex.InnerException.Message);
}

private class MySink : ILogEventSink
class UnimplementedSink : ILogEventSink
{
public void Emit(LogEvent logEvent)
{
throw new NotImplementedException("Oops");
}
}

[Fact]
public void TraceAndSpanIdsAreCaptured()
{
#if FORCE_W3C_ACTIVITY_ID
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
Activity.ForceDefaultIdFormat = true;
#endif

using var listener = new ActivityListener();
listener.ShouldListenTo = _ => true;
listener.Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData;

ActivitySource.AddActivityListener(listener);

var source = new ActivitySource("test.activity", "1.0.0");
using var activity = source.StartActivity();
Assert.NotNull(Activity.Current);

var (logger, sink) = SetUp(LogLevel.Trace);
logger.LogInformation("Hello trace and span!");

var evt = Assert.Single(sink.Writes);

Assert.Equal(Activity.Current.TraceId, evt.TraceId);
Assert.Equal(Activity.Current.SpanId, evt.SpanId);
}
}