Skip to content

Commit 2138e8c

Browse files
authored
Merge pull request #235 from serilog/dev
8.0.0 Release
2 parents 710777b + 26c840d commit 2138e8c

File tree

11 files changed

+92
-32
lines changed

11 files changed

+92
-32
lines changed

Build.ps1

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
echo "build: Build started"
22

3+
$env:Path = "$pwd/.dotnetcli;$env:Path"
4+
35
Push-Location $PSScriptRoot
46

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

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

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

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

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

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

5355
Pop-Location
5456
}

Setup.ps1

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
$ErrorActionPreference = "Stop"
2+
3+
$RequiredDotnetVersion = $(cat ./global.json | convertfrom-json).sdk.version
4+
5+
New-Item -ItemType Directory -Force "./build/" | Out-Null
6+
7+
Invoke-WebRequest "https://dot.net/v1/dotnet-install.ps1" -OutFile "./build/installcli.ps1"
8+
& ./build/installcli.ps1 -InstallDir "$pwd/.dotnetcli" -NoPath -Version $RequiredDotnetVersion
9+
if ($LASTEXITCODE) { throw ".NET install failed" }

appveyor.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ version: '{build}'
22
skip_tags: true
33
image: Visual Studio 2022
44
install:
5-
- ps: mkdir -Force ".\build\" | Out-Null
5+
- pwsh: ./Setup.ps1
6+
- pwsh: mkdir -Force ".\build\" | Out-Null
67
build_script:
7-
- ps: ./Build.ps1
8+
- pwsh: ./Build.ps1
89
test: off
910
artifacts:
1011
- path: artifacts/Serilog.*.nupkg

global.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"sdk": {
3+
"version": "8.0.100"
4+
}
5+
}

samples/Sample/Sample.csproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net7.0</TargetFramework>
4+
<TargetFramework>net8.0</TargetFramework>
55
<AssemblyName>Sample</AssemblyName>
66
<OutputType>Exe</OutputType>
77
<ImplicitUsings>enable</ImplicitUsings>
@@ -12,9 +12,9 @@
1212
</ItemGroup>
1313

1414
<ItemGroup>
15-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
16-
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
17-
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
15+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
16+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
17+
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.0" />
1818
</ItemGroup>
1919

2020
</Project>

serilog-extensions-logging.sln

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{9C21B9
2424
Directory.Build.targets = Directory.Build.targets
2525
README.md = README.md
2626
assets\Serilog.snk = assets\Serilog.snk
27+
build.sh = build.sh
28+
Setup.ps1 = Setup.ps1
29+
global.json = global.json
2730
EndProjectSection
2831
EndProject
2932
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}"

src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogger.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Reflection;
99
using Serilog.Debugging;
1010
using System.Collections.Concurrent;
11+
using System.Diagnostics;
1112

1213
namespace Serilog.Extensions.Logging;
1314

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

160+
var (traceId, spanId) = Activity.Current is { } activity ?
161+
(activity.TraceId, activity.SpanId) :
162+
(default(ActivityTraceId), default(ActivitySpanId));
163+
159164
var parsedTemplate = MessageTemplateParser.Parse(messageTemplate ?? "");
160-
return new LogEvent(DateTimeOffset.Now, level, exception, parsedTemplate, properties);
165+
return new LogEvent(DateTimeOffset.Now, level, exception, parsedTemplate, properties, traceId, spanId);
161166
}
162167

163168
static object? AsLoggableValue<TState>(TState state, Func<TState, Exception?, string>? formatter)

src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
<PropertyGroup>
44
<Description>Low-level Serilog provider for Microsoft.Extensions.Logging</Description>
55
<!-- This must match the major and minor components of the referenced Microsoft.Extensions.Logging package. -->
6-
<VersionPrefix>7.0.0</VersionPrefix>
6+
<VersionPrefix>8.0.0</VersionPrefix>
77
<Authors>Microsoft;Serilog Contributors</Authors>
88
<!-- These must match the Dependencies tab in https://www.nuget.org/packages/microsoft.extensions.logging at
99
the target version. -->
10-
<TargetFrameworks>net462;netstandard2.0;netstandard2.1;net6.0;net7.0</TargetFrameworks>
10+
<TargetFrameworks>net462;netstandard2.0;netstandard2.1;net6.0;net7.0;net8.0</TargetFrameworks>
1111
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1212
<PackageTags>serilog;Microsoft.Extensions.Logging</PackageTags>
1313
<PackageIcon>serilog-extension-nuget.png</PackageIcon>
@@ -29,9 +29,9 @@
2929
<None Include="..\..\assets\serilog-extension-nuget.png" Pack="true" PackagePath="" Visible="false" />
3030
<None Include="..\..\README.md" Pack="true" PackagePath="\" Visible="false" />
3131
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
32-
<PackageReference Include="Serilog" Version="2.12.0" />
32+
<PackageReference Include="Serilog" Version="3.1.1" />
3333
<!-- The version of this reference must match the major and minor components of the package version prefix. -->
34-
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
34+
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
3535
<PackageReference Include="Nullable" Version="1.3.1" PrivateAssets="All" />
3636
</ItemGroup>
3737

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net7.0</TargetFramework>
4+
<TargetFramework>net8.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
</PropertyGroup>
77

@@ -10,10 +10,10 @@
1010
</ItemGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
14-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" PrivateAssets="All" />
15-
<PackageReference Include="xunit" Version="2.4.2" />
16-
<PackageReference Include="BenchmarkDotNet" Version="0.13.5" />
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
14+
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" PrivateAssets="All" />
15+
<PackageReference Include="xunit" Version="2.6.1" />
16+
<PackageReference Include="BenchmarkDotNet" Version="0.13.10" />
1717
</ItemGroup>
1818

1919
</Project>

test/Serilog.Extensions.Logging.Tests/Serilog.Extensions.Logging.Tests.csproj

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net7.0;net472</TargetFrameworks>
4+
<TargetFrameworks>net8.0;net48</TargetFrameworks>
55
<ImplicitUsings>enable</ImplicitUsings>
66
</PropertyGroup>
77

8+
<PropertyGroup Condition=" '$(TargetFramework)' == 'net48' ">
9+
<DefineConstants>$(DefineConstants);FORCE_W3C_ACTIVITY_ID</DefineConstants>
10+
</PropertyGroup>
11+
812
<ItemGroup>
913
<ProjectReference Include="..\..\src\Serilog.Extensions.Logging\Serilog.Extensions.Logging.csproj" />
1014
</ItemGroup>
1115

1216
<ItemGroup>
13-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
14-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" PrivateAssets="all" />
15-
<PackageReference Include="xunit" Version="2.4.2" />
16-
<PackageReference Include="Shouldly" Version="4.1.0" />
17+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
18+
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" PrivateAssets="all" />
19+
<PackageReference Include="xunit" Version="2.6.1" />
20+
<PackageReference Include="Shouldly" Version="4.2.1" />
1721
<PackageReference Include="PublicApiGenerator" Version="11.0.0" />
1822
</ItemGroup>
1923

test/Serilog.Extensions.Logging.Tests/SerilogLoggerTests.cs

+39-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System.Collections;
5+
using System.Diagnostics;
56
using Serilog.Events;
67
using Microsoft.Extensions.Logging;
78
using Serilog.Debugging;
@@ -138,11 +139,11 @@ public void LogsCorrectMessage()
138139

139140
logger.Log<object>(LogLevel.Information, 0, null!, null!, null!);
140141
logger.Log(LogLevel.Information, 0, TestMessage, null!, null!);
141-
logger.Log<object>(LogLevel.Information, 0, null!, null!, (_, __) => TestMessage);
142+
logger.Log<object>(LogLevel.Information, 0, null!, null!, (_, _) => TestMessage);
142143

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

145-
Assert.Equal(1, sink.Writes[0].Properties.Count);
146+
Assert.Single(sink.Writes[0].Properties);
146147
Assert.Empty(sink.Writes[0].RenderMessage());
147148

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

310311
[Fact]
311-
public void BeginScopeDestructuresObjectsWhenDestructurerIsUsedInMessageTemplate()
312+
public void BeginScopeDestructuresObjectsWhenCapturingOperatorIsUsedInMessageTemplate()
312313
{
313314
var (logger, sink) = SetUp(LogLevel.Trace);
314315

@@ -328,7 +329,7 @@ public void BeginScopeDestructuresObjectsWhenDestructurerIsUsedInMessageTemplate
328329
}
329330

330331
[Fact]
331-
public void BeginScopeDestructuresObjectsWhenDestructurerIsUsedInDictionary()
332+
public void BeginScopeDestructuresObjectsWhenCapturingOperatorIsUsedInDictionary()
332333
{
333334
var (logger, sink) = SetUp(LogLevel.Trace);
334335

@@ -348,7 +349,7 @@ public void BeginScopeDestructuresObjectsWhenDestructurerIsUsedInDictionary()
348349
}
349350

350351
[Fact]
351-
public void BeginScopeDoesNotModifyKeyWhenDestructurerIsNotUsedInMessageTemplate()
352+
public void BeginScopeDoesNotModifyKeyWhenCapturingOperatorIsNotUsedInMessageTemplate()
352353
{
353354
var (logger, sink) = SetUp(LogLevel.Trace);
354355

@@ -362,7 +363,7 @@ public void BeginScopeDoesNotModifyKeyWhenDestructurerIsNotUsedInMessageTemplate
362363
}
363364

364365
[Fact]
365-
public void BeginScopeDoesNotModifyKeyWhenDestructurerIsNotUsedInDictionary()
366+
public void BeginScopeDoesNotModifyKeyWhenCapturingOperatorIsNotUsedInDictionary()
366367
{
367368
var (logger, sink) = SetUp(LogLevel.Trace);
368369

@@ -466,7 +467,10 @@ public void MismatchedMessageTemplateParameterCountIsHandled()
466467
{
467468
var (logger, sink) = SetUp(LogLevel.Trace);
468469

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

471475
Assert.Empty(sink.Writes);
472476
}
@@ -475,7 +479,7 @@ public void MismatchedMessageTemplateParameterCountIsHandled()
475479
public void ExceptionFromAuditSinkIsUnhandled()
476480
{
477481
var serilogLogger = new LoggerConfiguration()
478-
.AuditTo.Sink(new MySink())
482+
.AuditTo.Sink(new UnimplementedSink())
479483
.CreateLogger();
480484

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

489-
private class MySink : ILogEventSink
493+
class UnimplementedSink : ILogEventSink
490494
{
491495
public void Emit(LogEvent logEvent)
492496
{
493497
throw new NotImplementedException("Oops");
494498
}
495499
}
500+
501+
[Fact]
502+
public void TraceAndSpanIdsAreCaptured()
503+
{
504+
#if FORCE_W3C_ACTIVITY_ID
505+
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
506+
Activity.ForceDefaultIdFormat = true;
507+
#endif
508+
509+
using var listener = new ActivityListener();
510+
listener.ShouldListenTo = _ => true;
511+
listener.Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData;
512+
513+
ActivitySource.AddActivityListener(listener);
514+
515+
var source = new ActivitySource("test.activity", "1.0.0");
516+
using var activity = source.StartActivity();
517+
Assert.NotNull(Activity.Current);
518+
519+
var (logger, sink) = SetUp(LogLevel.Trace);
520+
logger.LogInformation("Hello trace and span!");
521+
522+
var evt = Assert.Single(sink.Writes);
523+
524+
Assert.Equal(Activity.Current.TraceId, evt.TraceId);
525+
Assert.Equal(Activity.Current.SpanId, evt.SpanId);
526+
}
496527
}

0 commit comments

Comments
 (0)