Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logformatter null check #2200

Merged
merged 3 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/OpenTelemetry/Logs/OpenTelemetryLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
this.categoryName,
logLevel,
eventId,
options.IncludeFormattedMessage ? formatter(state, exception) : null,
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
options.IncludeFormattedMessage ? formatter?.Invoke(state, exception) : null,
options.ParseStateValues ? null : (object)state,
exception,
options.ParseStateValues ? this.ParseState(state) : null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
47 changes: 26 additions & 21 deletions test/OpenTelemetry.Tests/Logs/LogRecordTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

#if !NET461
#if NETCOREAPP2_1
using Microsoft.Extensions.DependencyInjection;
#endif
using System;
using System.Collections;
using System.Collections.Generic;
Expand All @@ -36,11 +32,7 @@ public sealed class LogRecordTest : IDisposable
{
private readonly ILogger logger;
private readonly List<LogRecord> exportedItems = new List<LogRecord>();
#if NETCOREAPP2_1
private readonly ServiceProvider serviceProvider;
#else
private readonly ILoggerFactory loggerFactory;
#endif
private readonly BaseExportProcessor<LogRecord> processor;
private readonly BaseExporter<LogRecord> exporter;
private OpenTelemetryLoggerOptions options;
Expand All @@ -49,11 +41,7 @@ public LogRecordTest()
{
this.exporter = new InMemoryExporter<LogRecord>(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 =>
{
Expand All @@ -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<ILogger<LogRecordTest>>();
#else
this.logger = this.loggerFactory.CreateLogger<LogRecordTest>();
#endif
}

[Fact]
Expand Down Expand Up @@ -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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jviau - Please review!

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()
{
Expand Down Expand Up @@ -598,11 +607,7 @@ public void ParseStateValuesUsingCustomTest()

public void Dispose()
{
#if NETCOREAPP2_1
this.serviceProvider?.Dispose();
#else
this.loggerFactory?.Dispose();
#endif
}

internal struct Food
Expand Down