diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index 988d8b0216e..89595afcbd8 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -5,6 +5,9 @@ * Removes upper constraint for Microsoft.Extensions.Logging dependencies. ([#2179](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2179)) +* OpenTelemetryLogger modified to not throw, when the + formatter supplied in ILogger.Log call is null. ([#2200](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2200)) + ## 1.2.0-alpha1 Released 2021-Jul-23 diff --git a/src/OpenTelemetry/Logs/OpenTelemetryLogger.cs b/src/OpenTelemetry/Logs/OpenTelemetryLogger.cs index bc0cfeb25ba..11ca152d64a 100644 --- a/src/OpenTelemetry/Logs/OpenTelemetryLogger.cs +++ b/src/OpenTelemetry/Logs/OpenTelemetryLogger.cs @@ -53,7 +53,7 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except this.categoryName, logLevel, eventId, - options.IncludeFormattedMessage ? formatter(state, exception) : null, + options.IncludeFormattedMessage ? formatter?.Invoke(state, exception) : null, options.ParseStateValues ? null : (object)state, exception, options.ParseStateValues ? this.ParseState(state) : null); diff --git a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs index 2bb95b82f8a..5fb47ca84cb 100644 --- a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs +++ b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs @@ -31,9 +31,7 @@ using OpenTelemetry.Instrumentation.AspNetCore.Implementation; using OpenTelemetry.Tests; using OpenTelemetry.Trace; -#if NETCOREAPP2_1 -using TestApp.AspNetCore._2._1; -#elif NETCOREAPP3_1 +#if NETCOREAPP3_1 using TestApp.AspNetCore._3._1; #else using TestApp.AspNetCore._5._0; diff --git a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/DependencyInjectionConfigTests.cs b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/DependencyInjectionConfigTests.cs index 5c615b6d462..dfd8ad2b77b 100644 --- a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/DependencyInjectionConfigTests.cs +++ b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/DependencyInjectionConfigTests.cs @@ -20,9 +20,7 @@ using Microsoft.AspNetCore.TestHost; using Microsoft.Extensions.DependencyInjection; using OpenTelemetry.Trace; -#if NETCOREAPP2_1 -using TestApp.AspNetCore._2._1; -#elif NETCOREAPP3_1 +#if NETCOREAPP3_1 using TestApp.AspNetCore._3._1; #else using TestApp.AspNetCore._5._0; diff --git a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/IncomingRequestsCollectionsIsAccordingToTheSpecTests.cs b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/IncomingRequestsCollectionsIsAccordingToTheSpecTests.cs index cbfb66ea766..8f5ba8bd95c 100644 --- a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/IncomingRequestsCollectionsIsAccordingToTheSpecTests.cs +++ b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/IncomingRequestsCollectionsIsAccordingToTheSpecTests.cs @@ -25,9 +25,7 @@ using Microsoft.Extensions.DependencyInjection; using Moq; using OpenTelemetry.Trace; -#if NETCOREAPP2_1 -using TestApp.AspNetCore._2._1; -#elif NETCOREAPP3_1 +#if NETCOREAPP3_1 using TestApp.AspNetCore._3._1; #else using TestApp.AspNetCore._5._0; diff --git a/test/OpenTelemetry.Tests/Logs/LogRecordTest.cs b/test/OpenTelemetry.Tests/Logs/LogRecordTest.cs index 3e733bc37b8..f0e40724e2d 100644 --- a/test/OpenTelemetry.Tests/Logs/LogRecordTest.cs +++ b/test/OpenTelemetry.Tests/Logs/LogRecordTest.cs @@ -13,11 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // - #if !NET461 -#if NETCOREAPP2_1 -using Microsoft.Extensions.DependencyInjection; -#endif using System; using System.Collections; using System.Collections.Generic; @@ -36,11 +32,7 @@ public sealed class LogRecordTest : IDisposable { private readonly ILogger logger; private readonly List exportedItems = new List(); -#if NETCOREAPP2_1 - private readonly ServiceProvider serviceProvider; -#else private readonly ILoggerFactory loggerFactory; -#endif private readonly BaseExportProcessor processor; private readonly BaseExporter exporter; private OpenTelemetryLoggerOptions options; @@ -49,11 +41,7 @@ public LogRecordTest() { this.exporter = new InMemoryExporter(this.exportedItems); this.processor = new TestLogRecordProcessor(this.exporter); -#if NETCOREAPP2_1 - var serviceCollection = new ServiceCollection().AddLogging(builder => -#else this.loggerFactory = LoggerFactory.Create(builder => -#endif { builder.AddOpenTelemetry(options => { @@ -64,12 +52,7 @@ public LogRecordTest() builder.AddFilter(typeof(LogRecordTest).FullName, LogLevel.Trace); }); -#if NETCOREAPP2_1 - this.serviceProvider = serviceCollection.BuildServiceProvider(); - this.logger = this.serviceProvider.GetRequiredService>(); -#else this.logger = this.loggerFactory.CreateLogger(); -#endif } [Fact] @@ -339,6 +322,32 @@ public void IncludeFormattedMessageTest() } } + [Fact] + public void IncludeFormattedMessageTestWhenFormatterNull() + { + this.logger.Log(LogLevel.Information, default, "Hello World!", null, null); + var logRecord = this.exportedItems[0]; + Assert.Null(logRecord.FormattedMessage); + + this.options.IncludeFormattedMessage = true; + try + { + // Pass null as formatter function + this.logger.Log(LogLevel.Information, default, "Hello World!", null, null); + logRecord = this.exportedItems[1]; + Assert.Null(logRecord.FormattedMessage); + + var expectedFormattedMessage = "formatted message"; + this.logger.Log(LogLevel.Information, default, "Hello World!", null, (state, ex) => expectedFormattedMessage); + logRecord = this.exportedItems[2]; + Assert.Equal(expectedFormattedMessage, logRecord.FormattedMessage); + } + finally + { + this.options.IncludeFormattedMessage = false; + } + } + [Fact] public void IncludeScopesTest() { @@ -598,11 +607,7 @@ public void ParseStateValuesUsingCustomTest() public void Dispose() { -#if NETCOREAPP2_1 - this.serviceProvider?.Dispose(); -#else this.loggerFactory?.Dispose(); -#endif } internal struct Food