-
Notifications
You must be signed in to change notification settings - Fork 772
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
Logformatter null check #2200
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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 => | ||
{ | ||
|
@@ -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] | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
{ | ||
|
@@ -598,11 +607,7 @@ public void ParseStateValuesUsingCustomTest() | |
|
||
public void Dispose() | ||
{ | ||
#if NETCOREAPP2_1 | ||
this.serviceProvider?.Dispose(); | ||
#else | ||
this.loggerFactory?.Dispose(); | ||
#endif | ||
} | ||
|
||
internal struct Food | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we consider this as a bug?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good qn. After seeing some other implementations, which throw in Log(...formatter=null)., I am thinking we should follow the regular pattern of "not throwing from instrumentation APIs after successful initialization". So Otel logging provider can chose to not throw, end user may still see exception from other providers they have enabled.
And I can reword the changelog to just say:
"OpenTelemetry Logger modified to not throw when formatter is null"