Skip to content

Commit e43027e

Browse files
authored
Small tweaks to result execution (#28229)
- Don't allocate string for type if logging isn't on - Use pattern matching
1 parent 27610b0 commit e43027e

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

src/Mvc/Mvc.Core/src/Infrastructure/ObjectResultExecutor.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,12 @@ static Log()
196196
}
197197

198198
public static void BufferingAsyncEnumerable(ILogger logger, object asyncEnumerable)
199-
=> _bufferingAsyncEnumerable(logger, asyncEnumerable.GetType().FullName, null);
199+
{
200+
if (logger.IsEnabled(LogLevel.Debug))
201+
{
202+
_bufferingAsyncEnumerable(logger, asyncEnumerable.GetType().FullName, null);
203+
}
204+
}
200205
}
201206
}
202207
}

src/Mvc/Mvc.Core/src/Infrastructure/SystemTextJsonResultExecutor.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -126,7 +126,7 @@ private JsonSerializerOptions GetSerializerOptions(JsonResult result)
126126
}
127127
else
128128
{
129-
if (!(serializerSettings is JsonSerializerOptions settingsFromResult))
129+
if (serializerSettings is not JsonSerializerOptions settingsFromResult)
130130
{
131131
throw new InvalidOperationException(Resources.FormatProperty_MustBeInstanceOfType(
132132
nameof(JsonResult),
@@ -152,12 +152,20 @@ private static class Log
152152

153153
public static void JsonResultExecuting(ILogger logger, object value)
154154
{
155-
var type = value == null ? "null" : value.GetType().FullName;
156-
_jsonResultExecuting(logger, type, null);
155+
if (logger.IsEnabled(LogLevel.Information))
156+
{
157+
var type = value == null ? "null" : value.GetType().FullName;
158+
_jsonResultExecuting(logger, type, null);
159+
}
157160
}
158161

159162
public static void BufferingAsyncEnumerable(ILogger logger, object asyncEnumerable)
160-
=> _bufferingAsyncEnumerable(logger, asyncEnumerable.GetType().FullName, null);
163+
{
164+
if (logger.IsEnabled(LogLevel.Debug))
165+
{
166+
_bufferingAsyncEnumerable(logger, asyncEnumerable.GetType().FullName, null);
167+
}
168+
}
161169
}
162170
}
163171
}

src/Mvc/Mvc.NewtonsoftJson/src/NewtonsoftJsonResultExecutor.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,20 @@ static Log()
196196

197197
public static void JsonResultExecuting(ILogger logger, object value)
198198
{
199-
var type = value == null ? "null" : value.GetType().FullName;
200-
_jsonResultExecuting(logger, type, null);
199+
if (logger.IsEnabled(LogLevel.Information))
200+
{
201+
var type = value == null ? "null" : value.GetType().FullName;
202+
_jsonResultExecuting(logger, type, null);
203+
}
201204
}
202205

203206
public static void BufferingAsyncEnumerable(ILogger logger, object asyncEnumerable)
204-
=> _bufferingAsyncEnumerable(logger, asyncEnumerable.GetType().FullName, null);
207+
{
208+
if (logger.IsEnabled(LogLevel.Debug))
209+
{
210+
_bufferingAsyncEnumerable(logger, asyncEnumerable.GetType().FullName, null);
211+
}
212+
}
205213
}
206214
}
207215
}

0 commit comments

Comments
 (0)