Skip to content

Commit f7af03d

Browse files
committed
feat: Add operation name to log events using Activity context
1 parent fc1ba91 commit f7af03d

4 files changed

Lines changed: 37 additions & 23 deletions

File tree

src/Serilog.Sinks.ApplicationInsights/LoggerConfigurationApplicationInsightsExtensions.cs

Lines changed: 13 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,10 +91,13 @@ 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);
98-
}
94+
var wrapper = LoggerSinkConfiguration.Wrap(
95+
wrappedSink => new ApplicationInsightsSink(telemetryClient, telemetryConverter),
96+
configure => { }
97+
);
9998

99+
return loggerConfiguration.Sink(wrapper, restrictedToMinimumLevel, levelSwitch);
100+
}
100101

101102
/// <summary>
102103
/// Adds a Serilog sink that writes <see cref="LogEvent">log events</see> to Microsoft Application Insights
@@ -125,7 +126,6 @@ public static LoggerConfiguration ApplicationInsights(
125126

126127
var client = new TelemetryClient(config);
127128

128-
return loggerConfiguration.Sink(new ApplicationInsightsSink(client, telemetryConverter),
129-
restrictedToMinimumLevel, levelSwitch);
129+
return loggerConfiguration.ApplicationInsights(client, telemetryConverter, restrictedToMinimumLevel, levelSwitch);
130130
}
131-
}
131+
}

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +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

1515
using System;
16+
using System.Diagnostics;
1617
using System.Reflection;
1718
using System.Runtime.ExceptionServices;
1819
using System.Threading;
@@ -101,6 +102,15 @@ public virtual void Emit(LogEvent logEvent)
101102

102103
try
103104
{
105+
var activity = Activity.Current;
106+
if (activity is { OperationName: not null })
107+
{
108+
logEvent.AddOrUpdateProperty(
109+
new LogEventProperty(
110+
TelemetryConverterBase.OperationNameProperty,
111+
new ScalarValue(activity.OperationName)));
112+
}
113+
104114
var telemetries = _telemetryConverter.Convert(logEvent, _formatProvider);
105115

106116
// if 'null' is returned (& we therefore there's nothing to track), the logEvent is basically skipped
@@ -121,7 +131,7 @@ public virtual void Emit(LogEvent logEvent)
121131

122132
#endregion
123133

124-
#region AI specifc Helper methods
134+
#region AI specific Helper methods
125135

126136
/// <summary>
127137
/// Hands over the <paramref name="telemetry" /> to the AI telemetry client.
@@ -139,7 +149,7 @@ protected virtual void TrackTelemetry(ITelemetry telemetry)
139149
_telemetryClient?.Track(telemetry);
140150
}
141151

142-
#endregion AI specifc Helper methods
152+
#endregion AI specific Helper methods
143153

144154
#region Implementation of IDisposable
145155

@@ -195,7 +205,7 @@ protected virtual void Dispose(bool disposeManagedResources)
195205
IsDisposing = false;
196206
}
197207
}
198-
208+
199209
#if NET6_0_OR_GREATER
200210
/// <summary>
201211
/// Disposes the sink and flushes telemetry to App Insights.
@@ -204,7 +214,7 @@ public async ValueTask DisposeAsync()
204214
{
205215
if (IsDisposing || IsDisposed)
206216
return;
207-
217+
208218
try
209219
{
210220
IsDisposing = true;
@@ -220,4 +230,4 @@ public async ValueTask DisposeAsync()
220230
#endif
221231

222232
#endregion Implementation of IDisposable
223-
}
233+
}

test/Serilog.Sinks.ApplicationInsights.Tests/EventTelemetryConverterTest.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ public void ParentSpanIdIsSet()
7676
[Fact]
7777
public void OperationNameIsSet()
7878
{
79-
Logger.Information("Test {OperationName}", "MyOperation");
79+
using Activity activity = new("MyOperation");
80+
activity.Start();
81+
Logger.Information("Test");
8082
Assert.Equal("MyOperation", LastSubmittedEventTelemetry.Context.Operation.Name);
8183
}
8284

test/Serilog.Sinks.ApplicationInsights.Tests/TraceTelemetryConverterTest.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ public void ParentSpanIdIsSet()
7676
[Fact]
7777
public void OperationNameIsSet()
7878
{
79-
Logger.Information("Test {OperationName}", "MyOperation");
79+
using Activity activity = new("MyOperation");
80+
activity.Start();
81+
Logger.Information("Test");
8082
Assert.Equal("MyOperation", LastSubmittedTraceTelemetry.Context.Operation.Name);
8183
}
8284

0 commit comments

Comments
 (0)