Skip to content

Commit 3e5c53c

Browse files
author
David Obee
committed
Add experiement project demonstrating that tags and baggage do not appear (while Span and Trace IDs do)
1 parent 7dc58c3 commit 3e5c53c

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<OutputType>Exe</OutputType>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\..\src\Serilog.Extensions.Logging\Serilog.Extensions.Logging.csproj" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
17+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
18+
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.0" />
19+
</ItemGroup>
20+
21+
</Project>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System.Diagnostics;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Logging;
4+
using Serilog;
5+
using Serilog.Formatting.Json;
6+
7+
// Configure a JsonFormatter to log out scope to the console
8+
Log.Logger = new LoggerConfiguration()
9+
.MinimumLevel.Debug()
10+
.WriteTo.Console(new JsonFormatter())
11+
.CreateLogger();
12+
13+
// Setup Serilog with M.E.L, and configure the appropriate ActivityTrackingOptions
14+
var services = new ServiceCollection();
15+
16+
services.AddLogging(l => l
17+
.AddSerilog()
18+
.Configure(options =>
19+
{
20+
options.ActivityTrackingOptions = ActivityTrackingOptions.TraceId | ActivityTrackingOptions.Tags | ActivityTrackingOptions.Baggage;
21+
}));
22+
23+
// Add an ActivityListener (required, otherwise Activities don't actually get created if nothing is listening to them)
24+
ActivitySource.AddActivityListener(new ActivityListener
25+
{
26+
ShouldListenTo = source => true,
27+
Sample = (ref ActivityCreationOptions<ActivityContext> options) => ActivitySamplingResult.AllDataAndRecorded
28+
});
29+
30+
// Run our test
31+
var activitySource = new ActivitySource("SomeActivitySource");
32+
33+
var serviceProvider = services.BuildServiceProvider();
34+
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
35+
36+
using var activity = activitySource.StartActivity();
37+
38+
activity?.SetTag("tag.domain.id", 1234);
39+
activity?.SetBaggage("baggage.environment", "uat");
40+
41+
using var scope = logger.BeginScope(new
42+
{
43+
User = "Hugh Mann",
44+
Time = DateTimeOffset.UtcNow
45+
});
46+
47+
logger.LogInformation("Hello world!");
48+
49+
serviceProvider.Dispose();

serilog-extensions-logging.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{9C21B9
3131
EndProject
3232
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}"
3333
EndProject
34+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ActivityProofOfConcept", "samples\ActivityProofOfConcept\ActivityProofOfConcept.csproj", "{653092A8-CBAD-40AA-A4CE-F8B19D6492C2}"
35+
EndProject
3436
Global
3537
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3638
Debug|Any CPU = Debug|Any CPU
@@ -53,6 +55,10 @@ Global
5355
{6D5986FF-EECD-4E75-8BC6-A5F78AB549B2}.Debug|Any CPU.Build.0 = Debug|Any CPU
5456
{6D5986FF-EECD-4E75-8BC6-A5F78AB549B2}.Release|Any CPU.ActiveCfg = Release|Any CPU
5557
{6D5986FF-EECD-4E75-8BC6-A5F78AB549B2}.Release|Any CPU.Build.0 = Release|Any CPU
58+
{653092A8-CBAD-40AA-A4CE-F8B19D6492C2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
59+
{653092A8-CBAD-40AA-A4CE-F8B19D6492C2}.Debug|Any CPU.Build.0 = Debug|Any CPU
60+
{653092A8-CBAD-40AA-A4CE-F8B19D6492C2}.Release|Any CPU.ActiveCfg = Release|Any CPU
61+
{653092A8-CBAD-40AA-A4CE-F8B19D6492C2}.Release|Any CPU.Build.0 = Release|Any CPU
5662
EndGlobalSection
5763
GlobalSection(SolutionProperties) = preSolution
5864
HideSolutionNode = FALSE
@@ -62,6 +68,7 @@ Global
6268
{37EADF84-5E41-4224-A194-1E3299DCD0B8} = {E30F638E-BBBE-4AD1-93CE-48CC69CFEFE1}
6369
{65357FBC-9BC4-466D-B621-1C3A19BC2A78} = {F2407211-6043-439C-8E06-3641634332E7}
6470
{6D5986FF-EECD-4E75-8BC6-A5F78AB549B2} = {E30F638E-BBBE-4AD1-93CE-48CC69CFEFE1}
71+
{653092A8-CBAD-40AA-A4CE-F8B19D6492C2} = {F2407211-6043-439C-8E06-3641634332E7}
6572
EndGlobalSection
6673
GlobalSection(ExtensibilityGlobals) = postSolution
6774
SolutionGuid = {811E61C5-3871-4633-AFAE-B35B619C8A10}

0 commit comments

Comments
 (0)