Skip to content

Commit ad13e28

Browse files
authored
Reduce boxing in logging (#88560)
- Logging got overhauled to use CompositeFormat in .NET 8, this allows for non boxing string formatting via string.Format. There's one piece of code that boxing the generic argument before handing off to string.Format so remove it for primitive types and enums.
1 parent 54cb573 commit ad13e28

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LogValuesFormatter.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,15 @@ private static object FormatArgument(object? value)
263263
return TryFormatArgumentIfNullOrEnumerable(value, ref stringValue) ? stringValue : value!;
264264
}
265265

266-
private static bool TryFormatArgumentIfNullOrEnumerable(object? value, [NotNullWhen(true)] ref object? stringValue)
266+
private static bool TryFormatArgumentIfNullOrEnumerable<T>(T? value, [NotNullWhen(true)] ref object? stringValue)
267267
{
268+
// Avoiding boxing of known types
269+
if (typeof(T).IsPrimitive || typeof(T).IsEnum)
270+
{
271+
stringValue = null;
272+
return false;
273+
}
274+
268275
if (value == null)
269276
{
270277
stringValue = NullValue;

0 commit comments

Comments
 (0)