Skip to content

Commit f1a9c0d

Browse files
authored
stop considering log entries with LogLevel.None (#2667)
1 parent 6c4f37a commit f1a9c0d

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## VNext
44
- Update endpoint redirect header name for QuickPulse module to v2
55
- AzureSdkDiagnosticListener modified to use sdkversion prefix "rdddsaz" instead of "dotnet".
6+
- [ILogger logs with LogLevel.None severity are now ignored by ApplicationInsightsLogger](https://github.com/microsoft/ApplicationInsights-dotnet/pull/2667). Fixes ([#2666](https://github.com/microsoft/ApplicationInsights-dotnet/issues/2666))
67

78
## Version 2.21.0
89
- no changes since beta.

LOGGING/src/ILogger/ApplicationInsightsLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public IDisposable BeginScope<TState>(TState state)
6565
/// </returns>
6666
public bool IsEnabled(LogLevel logLevel)
6767
{
68-
return this.telemetryClient.IsEnabled();
68+
return logLevel != LogLevel.None && this.telemetryClient.IsEnabled();
6969
}
7070

7171
/// <summary>

LOGGING/test/ILogger.Tests/ILoggerIntegrationTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public void ApplicationInsightsLoggerIsInvokedWhenUsingILogger()
9696
testLogger.LogTrace("Trace");
9797
testLogger.LogWarning("Warning");
9898
testLogger.LogDebug("Debug");
99+
testLogger.Log(LogLevel.None, "None");
99100

100101
Assert.AreEqual(7, itemsReceived.Count);
101102

@@ -125,6 +126,37 @@ public void ApplicationInsightsLoggerIsInvokedWhenUsingILogger()
125126
Assert.AreEqual("Debug", (itemsReceived[6] as TraceTelemetry).Message);
126127
}
127128

129+
/// <summary>
130+
/// Ensures that <see cref="ApplicationInsightsLogger"/> is invoked when user logs using <see cref="ILogger"/>.
131+
/// </summary>
132+
[TestMethod]
133+
[TestCategory("ILogger")]
134+
public void ApplicationInsightsLoggerIsNotInvokedWhenUsingILoggerAndTelemetryIsDisabled()
135+
{
136+
List<ITelemetry> itemsReceived = new List<ITelemetry>();
137+
138+
IServiceProvider serviceProvider = ILoggerIntegrationTests.SetupApplicationInsightsLoggerIntegration((telemetryItem, telemetryProcessor) =>
139+
{
140+
itemsReceived.Add(telemetryItem);
141+
}, configuration =>
142+
{
143+
configuration.DisableTelemetry = true;
144+
});
145+
146+
ILogger<ILoggerIntegrationTests> testLogger = serviceProvider.GetRequiredService<ILogger<ILoggerIntegrationTests>>();
147+
148+
testLogger.LogInformation("Testing");
149+
testLogger.LogError(new Exception("ExceptionMessage"), "LoggerMessage");
150+
testLogger.LogInformation(new EventId(100, "TestEvent"), "TestingEvent");
151+
testLogger.LogCritical("Critical");
152+
testLogger.LogTrace("Trace");
153+
testLogger.LogWarning("Warning");
154+
testLogger.LogDebug("Debug");
155+
testLogger.Log(LogLevel.None, "None");
156+
157+
Assert.AreEqual(0, itemsReceived.Count);
158+
}
159+
128160
/// <summary>
129161
/// Ensures that the <see cref="ApplicationInsightsLoggerOptions.TrackExceptionsAsExceptionTelemetry"/> switch is honored
130162
/// and exceptions are logged as trace messages when value is true.

0 commit comments

Comments
 (0)