Skip to content

Commit 92b170c

Browse files
Ivan GavryliukIvan Gavryliuk
authored andcommitted
adding test that log context properties are included (#28)
1 parent 890dbc0 commit 92b170c

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

src/Serilog.Sinks.ApplicationInsights.Tests/ApplicationInsightsTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ protected ApplicationInsightsTest()
4141
Logger = new LoggerConfiguration()
4242
.WriteTo.ApplicationInsightsTraces(tc)
4343
.MinimumLevel.Debug()
44+
.Enrich.FromLogContext()
4445
.CreateLogger();
4546
}
4647

src/Serilog.Sinks.ApplicationInsights.Tests/FormattingTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.ApplicationInsights.Channel;
2+
using Serilog.Context;
23
using Xunit;
34

45
namespace Serilog.Sinks.ApplicationInsights.Tests
@@ -20,5 +21,16 @@ public void Message_template_is_not_in_trace_custom_property()
2021

2122
Assert.False(LastSubmittedTraceTelemetry.Properties.ContainsKey("MessageTemplate"));
2223
}
24+
25+
[Fact]
26+
public void Message_properies_include_log_context()
27+
{
28+
using (LogContext.PushProperty("custom1", "value1"))
29+
{
30+
Logger.Information("test context");
31+
32+
Assert.True(LastSubmittedTraceTelemetry.Properties.TryGetValue("custom1", out string value1) && value1 == "value1");
33+
}
34+
}
2335
}
2436
}

src/Serilog.Sinks.ApplicationInsights/Sinks/ApplicationInsights/ApplicationInsightsSinkBase.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ public virtual void Emit(LogEvent logEvent)
141141
{
142142
foreach (ITelemetry telemetry in telemetries)
143143
{
144-
TrackTelemetry(telemetry);
144+
if (telemetry != null)
145+
{
146+
TrackTelemetry(telemetry);
147+
}
145148
}
146149
}
147150
}

0 commit comments

Comments
 (0)