Skip to content

Commit e14ab27

Browse files
authored
Merge pull request #242 from serilog-contrib/dev
5.0.0 Release
2 parents db06b05 + 27d2873 commit e14ab27

32 files changed

Lines changed: 927 additions & 135 deletions

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,6 @@ csharp_space_around_declaration_statements = false
4949
csharp_space_before_open_square_brackets = false
5050
csharp_space_between_empty_square_brackets = false
5151
csharp_space_between_square_brackets = false
52+
53+
# C# code style - Accessibility modifiers
54+
dotnet_style_require_accessibility_modifiers = omit_if_default

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ jobs:
2222
contents: write
2323

2424
steps:
25-
- uses: actions/checkout@v4
25+
- uses: actions/checkout@v6
2626
- name: Setup
27-
uses: actions/setup-dotnet@v4
27+
uses: actions/setup-dotnet@v5
2828
with:
2929
global-json-file: global.json
3030
- name: Compute build number

Directory.Version.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<Project>
22
<PropertyGroup>
3-
<VersionPrefix>4.1.0</VersionPrefix>
3+
<VersionPrefix>5.0.0</VersionPrefix>
44
</PropertyGroup>
55
</Project>

README.md

Lines changed: 115 additions & 39 deletions
Large diffs are not rendered by default.

global.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
22
"sdk": {
3-
"version": "9.0.100",
3+
"version": "10.0.100",
44
"allowPrerelease": false,
55
"rollForward": "latestFeature"
66
}
77
}
8-

src/Serilog.Sinks.ApplicationInsights/LoggerConfigurationApplicationInsightsExtensions.cs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Copyright 2016 Serilog Contributors
2-
//
2+
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
55
// You may obtain a copy of the License at
6-
//
6+
//
77
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
8+
//
99
// Unless required by applicable law or agreed to in writing, software
1010
// distributed under the License is distributed on an "AS IS" BASIS,
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -49,8 +49,7 @@ public static LoggerConfiguration ApplicationInsights(
4949
var client = new TelemetryClient(telemetryConfiguration ?? TelemetryConfiguration.Active);
5050
#pragma warning restore CS0618
5151

52-
return loggerConfiguration.Sink(new ApplicationInsightsSink(client, telemetryConverter),
53-
restrictedToMinimumLevel, levelSwitch);
52+
return loggerConfiguration.ApplicationInsights(client, telemetryConverter, restrictedToMinimumLevel, levelSwitch);
5453
}
5554

5655
/// <summary>
@@ -72,8 +71,7 @@ public static LoggerConfiguration ApplicationInsights(
7271
var client = new TelemetryClient(TelemetryConfiguration.Active);
7372
#pragma warning restore CS0618
7473

75-
return loggerConfiguration.Sink(new ApplicationInsightsSink(client, telemetryConverter),
76-
restrictedToMinimumLevel, levelSwitch);
74+
return loggerConfiguration.ApplicationInsights(client, telemetryConverter, restrictedToMinimumLevel, levelSwitch);
7775
}
7876

7977
/// <summary>
@@ -93,11 +91,9 @@ public static LoggerConfiguration ApplicationInsights(
9391
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
9492
LoggingLevelSwitch levelSwitch = null)
9593
{
96-
return loggerConfiguration.Sink(new ApplicationInsightsSink(telemetryClient, telemetryConverter),
97-
restrictedToMinimumLevel, levelSwitch);
94+
return loggerConfiguration.Sink(new ApplicationInsightsSink(telemetryClient, telemetryConverter), restrictedToMinimumLevel, levelSwitch);
9895
}
9996

100-
10197
/// <summary>
10298
/// Adds a Serilog sink that writes <see cref="LogEvent">log events</see> to Microsoft Application Insights
10399
/// using a custom <see cref="ITelemetry" /> converter / constructor. Only use in rare cases when your application
@@ -125,7 +121,6 @@ public static LoggerConfiguration ApplicationInsights(
125121

126122
var client = new TelemetryClient(config);
127123

128-
return loggerConfiguration.Sink(new ApplicationInsightsSink(client, telemetryConverter),
129-
restrictedToMinimumLevel, levelSwitch);
124+
return loggerConfiguration.ApplicationInsights(client, telemetryConverter, restrictedToMinimumLevel, levelSwitch);
130125
}
131-
}
126+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// SPDX-FileCopyrightText: 2025 Serilog Contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using System.Diagnostics;
5+
using Serilog.Configuration;
6+
using Serilog.Sinks.ApplicationInsights.Enrichers;
7+
8+
namespace Serilog;
9+
10+
/// <summary>
11+
/// Provides enrichment extensions to <see cref="LoggerConfiguration" />.
12+
/// </summary>
13+
public static class LoggerEnrichmentConfigurationExtensions
14+
{
15+
extension(LoggerEnrichmentConfiguration loggerEnrichmentConfiguration)
16+
{
17+
/// <summary>
18+
/// Enriches log events with details from <see cref="Activity" />.
19+
/// </summary>
20+
/// <returns>Logger configuration, allowing configuration to continue.</returns>
21+
public LoggerConfiguration WithActivityDetails(bool includeOperationName = true, bool includeBaggage = true)
22+
=> loggerEnrichmentConfiguration.With(new ActivityDetailsEnricher(includeOperationName, includeBaggage));
23+
}
24+
}

src/Serilog.Sinks.ApplicationInsights/Serilog.Sinks.ApplicationInsights.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</PropertyGroup>
2424

2525
<ItemGroup>
26-
<PackageReference Include="Serilog" Version="4.2.0" />
26+
<PackageReference Include="Serilog" Version="4.3.0" />
2727
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.23.0" />
2828
</ItemGroup>
2929

src/Serilog.Sinks.ApplicationInsights/Sinks/ApplicationInsights/ApplicationInsightsSink.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
// Copyright 2016 Serilog Contributors
2-
//
2+
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
55
// You may obtain a copy of the License at
6-
//
6+
//
77
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
8+
//
99
// Unless required by applicable law or agreed to in writing, software
1010
// distributed under the License is distributed on an "AS IS" BASIS,
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
using System;
1615
using System.Reflection;
1716
using System.Runtime.ExceptionServices;
18-
using System.Threading;
19-
using System.Threading.Tasks;
2017
using Microsoft.ApplicationInsights;
2118
using Microsoft.ApplicationInsights.Channel;
2219
using Serilog.Core;
@@ -121,7 +118,7 @@ public virtual void Emit(LogEvent logEvent)
121118

122119
#endregion
123120

124-
#region AI specifc Helper methods
121+
#region AI specific Helper methods
125122

126123
/// <summary>
127124
/// Hands over the <paramref name="telemetry" /> to the AI telemetry client.
@@ -139,7 +136,7 @@ protected virtual void TrackTelemetry(ITelemetry telemetry)
139136
_telemetryClient?.Track(telemetry);
140137
}
141138

142-
#endregion AI specifc Helper methods
139+
#endregion AI specific Helper methods
143140

144141
#region Implementation of IDisposable
145142

@@ -195,7 +192,7 @@ protected virtual void Dispose(bool disposeManagedResources)
195192
IsDisposing = false;
196193
}
197194
}
198-
195+
199196
#if NET6_0_OR_GREATER
200197
/// <summary>
201198
/// Disposes the sink and flushes telemetry to App Insights.
@@ -204,7 +201,7 @@ public async ValueTask DisposeAsync()
204201
{
205202
if (IsDisposing || IsDisposed)
206203
return;
207-
204+
208205
try
209206
{
210207
IsDisposing = true;
@@ -220,4 +217,4 @@ public async ValueTask DisposeAsync()
220217
#endif
221218

222219
#endregion Implementation of IDisposable
223-
}
220+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// SPDX-FileCopyrightText: 2025 Serilog Contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using System.Diagnostics;
5+
using Serilog.Core;
6+
using Serilog.Events;
7+
using Serilog.Sinks.ApplicationInsights.TelemetryConverters;
8+
9+
namespace Serilog.Sinks.ApplicationInsights.Enrichers;
10+
11+
/// <summary>
12+
/// Enriches log events with details from <see cref="Activity"/>.
13+
/// </summary>
14+
public class ActivityDetailsEnricher(bool includeOperationName, bool includeBaggage) : ILogEventEnricher
15+
{
16+
readonly bool _includeOperationName = includeOperationName;
17+
readonly bool _includeBaggage = includeBaggage;
18+
19+
/// <inheritdoc/>
20+
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
21+
{
22+
if (logEvent == null)
23+
{
24+
throw new ArgumentNullException(nameof(logEvent));
25+
}
26+
27+
if (propertyFactory == null)
28+
{
29+
throw new ArgumentNullException(nameof(propertyFactory));
30+
}
31+
32+
if (Activity.Current is not { } activity)
33+
{
34+
return;
35+
}
36+
37+
EnrichOperationName(logEvent, propertyFactory, activity);
38+
EnrichBaggage(logEvent, propertyFactory, activity);
39+
}
40+
41+
private void EnrichOperationName(LogEvent logEvent, ILogEventPropertyFactory propertyFactory, Activity activity)
42+
{
43+
if (!_includeOperationName)
44+
{
45+
return;
46+
}
47+
48+
if (activity.OperationName is not string operationName)
49+
{
50+
return;
51+
}
52+
53+
LogEventProperty operationNameProperty = propertyFactory.CreateProperty(TelemetryConverterBase.OperationNameProperty, operationName);
54+
logEvent.AddPropertyIfAbsent(operationNameProperty);
55+
}
56+
57+
private void EnrichBaggage(LogEvent logEvent, ILogEventPropertyFactory propertyFactory, Activity activity)
58+
{
59+
if (!_includeBaggage)
60+
{
61+
return;
62+
}
63+
64+
IEnumerable<LogEventProperty> items = activity.Baggage
65+
.Where(IsValidBaggageItem)
66+
.Select(item => propertyFactory.CreateProperty(item.Key, item.Value));
67+
68+
LogEventProperty baggageProperty = new(TelemetryConverterBase.BaggageProperty, new StructureValue(items));
69+
logEvent.AddPropertyIfAbsent(baggageProperty);
70+
}
71+
72+
private static bool IsValidBaggageItem(KeyValuePair<string, string> item)
73+
=> !string.IsNullOrEmpty(item.Key) && !string.IsNullOrEmpty(item.Value);
74+
}

0 commit comments

Comments
 (0)