Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -192,32 +192,26 @@ internal string Format()
#if NET8_0_OR_GREATER
internal string Format<TArg0>(TArg0 arg0)
{
object? arg0String = null;
return
!TryFormatArgumentIfNullOrEnumerable(arg0, ref arg0String) ?
!TryFormatArgumentIfNullOrEnumerable(arg0, out object? arg0String) ?
string.Format(CultureInfo.InvariantCulture, _format, arg0) :
string.Format(CultureInfo.InvariantCulture, _format, arg0String);
}

internal string Format<TArg0, TArg1>(TArg0 arg0, TArg1 arg1)
{
object? arg0String = null, arg1String = null;
return
!TryFormatArgumentIfNullOrEnumerable(arg0, ref arg0String) &&
!TryFormatArgumentIfNullOrEnumerable(arg1, ref arg1String) ?
string.Format(CultureInfo.InvariantCulture, _format, arg0, arg1) :
string.Format(CultureInfo.InvariantCulture, _format, arg0String ?? arg0, arg1String ?? arg1);
}
TryFormatArgumentIfNullOrEnumerable(arg0, out object? arg0String) | TryFormatArgumentIfNullOrEnumerable(arg1, out object? arg1String) ?
string.Format(CultureInfo.InvariantCulture, _format, arg0String ?? arg0, arg1String ?? arg1) :
string.Format(CultureInfo.InvariantCulture, _format, arg0, arg1);
}

internal string Format<TArg0, TArg1, TArg2>(TArg0 arg0, TArg1 arg1, TArg2 arg2)
{
object? arg0String = null, arg1String = null, arg2String = null;
return
!TryFormatArgumentIfNullOrEnumerable(arg0, ref arg0String) &&
!TryFormatArgumentIfNullOrEnumerable(arg1, ref arg1String) &&
!TryFormatArgumentIfNullOrEnumerable(arg2, ref arg2String) ?
string.Format(CultureInfo.InvariantCulture, _format, arg0, arg1, arg2) :
string.Format(CultureInfo.InvariantCulture, _format, arg0String ?? arg0, arg1String ?? arg1, arg2String ?? arg2);
TryFormatArgumentIfNullOrEnumerable(arg0, out object? arg0String) | TryFormatArgumentIfNullOrEnumerable(arg1, out object? arg1String) | TryFormatArgumentIfNullOrEnumerable(arg2, out object? arg2String) ?
string.Format(CultureInfo.InvariantCulture, _format, arg0String ?? arg0, arg1String ?? arg1, arg2String ?? arg2):
string.Format(CultureInfo.InvariantCulture, _format, arg0, arg1, arg2);
}
#else
internal string Format(object? arg0) =>
Expand Down Expand Up @@ -259,11 +253,10 @@ internal string Format(object? arg0, object? arg1, object? arg2) =>

private static object FormatArgument(object? value)
{
object? stringValue = null;
return TryFormatArgumentIfNullOrEnumerable(value, ref stringValue) ? stringValue : value!;
return TryFormatArgumentIfNullOrEnumerable(value, out object? stringValue) ? stringValue : value!;
}

private static bool TryFormatArgumentIfNullOrEnumerable<T>(T? value, [NotNullWhen(true)] ref object? stringValue)
private static bool TryFormatArgumentIfNullOrEnumerable<T>(T? value, [NotNullWhen(true)] out object? stringValue)
{
if (value == null)
{
Expand All @@ -290,6 +283,7 @@ private static bool TryFormatArgumentIfNullOrEnumerable<T>(T? value, [NotNullWhe
return true;
}

stringValue = null;
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<EnableDefaultItems>true</EnableDefaultItems>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IsPackable>true</IsPackable>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<ServicingVersion>1</ServicingVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<ServicingVersion>2</ServicingVersion>
<PackageDescription>Logging abstractions for Microsoft.Extensions.Logging.

Commonly Used Types:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal static IEnumerable<ConsoleFormatter> GetFormatters(
var defaultMonitor = new TestFormatterOptionsMonitor<SimpleConsoleFormatterOptions>(simpleOptions ?? new SimpleConsoleFormatterOptions());
var systemdMonitor = new TestFormatterOptionsMonitor<ConsoleFormatterOptions>(systemdOptions ?? new ConsoleFormatterOptions());
var jsonMonitor = new TestFormatterOptionsMonitor<JsonConsoleFormatterOptions>(jsonOptions ?? new JsonConsoleFormatterOptions());
var formatters = new List<ConsoleFormatter>() {
var formatters = new List<ConsoleFormatter>() {
new SimpleConsoleFormatter(defaultMonitor),
new SystemdConsoleFormatter(systemdMonitor),
new JsonConsoleFormatter(jsonMonitor)
Expand Down Expand Up @@ -86,13 +86,13 @@ private static void VerifyDeprecatedPropertiesUsedOnNullFormatterName(ConsoleLog
Assert.Equal(formatter.FormatterOptions.IncludeScopes, logger.Options.IncludeScopes);
Assert.Equal(formatter.FormatterOptions.UseUtcTimestamp, logger.Options.UseUtcTimestamp);
Assert.Equal(formatter.FormatterOptions.TimestampFormat, logger.Options.TimestampFormat);
Assert.Equal(formatter.FormatterOptions.ColorBehavior,
logger.Options.DisableColors ? LoggerColorBehavior.Disabled : LoggerColorBehavior.Enabled);
Assert.Equal(formatter.FormatterOptions.ColorBehavior,
logger.Options.DisableColors ? LoggerColorBehavior.Disabled : LoggerColorBehavior.Enabled);
}
else
{
var formatter = Assert.IsType<SystemdConsoleFormatter>(logger.Formatter);
Assert.Equal(formatter.FormatterOptions.IncludeScopes, logger.Options.IncludeScopes);
Assert.Equal(formatter.FormatterOptions.IncludeScopes, logger.Options.IncludeScopes);
Assert.Equal(formatter.FormatterOptions.UseUtcTimestamp, logger.Options.UseUtcTimestamp);
Assert.Equal(formatter.FormatterOptions.TimestampFormat, logger.Options.TimestampFormat);
}
Expand All @@ -103,7 +103,7 @@ private static void UpdateFormatterOptions(ConsoleFormatter formatter, ConsoleLo
// kept for deprecated apis:
if (formatter is SimpleConsoleFormatter defaultFormatter)
{
defaultFormatter.FormatterOptions.ColorBehavior = deprecatedFromOptions.DisableColors ?
defaultFormatter.FormatterOptions.ColorBehavior = deprecatedFromOptions.DisableColors ?
LoggerColorBehavior.Disabled : LoggerColorBehavior.Enabled;
defaultFormatter.FormatterOptions.IncludeScopes = deprecatedFromOptions.IncludeScopes;
defaultFormatter.FormatterOptions.TimestampFormat = deprecatedFromOptions.TimestampFormat;
Expand Down Expand Up @@ -1346,6 +1346,31 @@ public void ConsoleLoggerOptions_IncludeScopes_IsReadFromLoggingConfiguration()
Assert.True(formatter.FormatterOptions.IncludeScopes);
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
public void LogMultipleArrays()
{
// Arrange
var t = SetUp();
var logger = t.Logger;
var sink = t.Sink;

var define1 = LoggerMessage.Define<string[], string[]>(LogLevel.Information, new EventId(), "Log: {Array1} and {Array2}");
var define2 = LoggerMessage.Define<int, string[], string[]>(LogLevel.Information, new EventId(), "Log {Number}: {Array1} and {Array2}");

// Act
define1(logger, ["a", "b", "c"], ["d", "e", "f"], null);
define2(logger, 30, ["a", "b", "c"], ["d", "e", "f"], null);

var expectedMessage1 = $"{CreateHeader(ConsoleLoggerFormat.Default)}{Environment.NewLine}{_paddingString}Log: a, b, c and d, e, f{Environment.NewLine}";
var expectedMessage2 = $"{CreateHeader(ConsoleLoggerFormat.Default)}{Environment.NewLine}{_paddingString}Log 30: a, b, c and d, e, f{Environment.NewLine}";

Assert.Equal(4, sink.Writes.Count);
Assert.Equal("info", sink.Writes[0].Message);
Assert.Equal(expectedMessage1, sink.Writes[1].Message);
Assert.Equal("info", sink.Writes[2].Message);
Assert.Equal(expectedMessage2, sink.Writes[3].Message);
}

public static TheoryData<ConsoleLoggerFormat, LogLevel> FormatsAndLevels
{
get
Expand Down