Skip to content

Commit d807bf0

Browse files
committed
Avoid some allocations by checked IsEnabled first in MVC
1 parent eec65ec commit d807bf0

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

src/Mvc/Mvc.Razor/src/MvcRazorLoggerExtensions.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ static MvcRazorLoggerExtensions()
4141
_viewCompilerEndCodeGeneration = LoggerMessage.Define<string, double>(
4242
LogLevel.Debug,
4343
new EventId(2, "ViewCompilerEndCodeGeneration"),
44-
"Code generation for the Razor file at '{FilePath}' completed in {ElapsedMilliseconds}ms.");
44+
"Code generation for the Razor file at '{FilePath}' completed in {ElapsedMilliseconds}ms.",
45+
skipEnabledCheck: true);
4546

4647
_viewCompilerLocatedCompiledView = LoggerMessage.Define<string>(
4748
LogLevel.Debug,
@@ -98,7 +99,8 @@ static MvcRazorLoggerExtensions()
9899
_generatedCodeToAssemblyCompilationEnd = LoggerMessage.Define<string, double>(
99100
LogLevel.Debug,
100101
new EventId(2, "GeneratedCodeToAssemblyCompilationEnd"),
101-
"Compilation of the generated code for the Razor file at '{FilePath}' completed in {ElapsedMilliseconds}ms.");
102+
"Compilation of the generated code for the Razor file at '{FilePath}' completed in {ElapsedMilliseconds}ms.",
103+
skipEnabledCheck: true);
102104

103105
_tagHelperComponentInitialized = LoggerMessage.Define<string>(
104106
LogLevel.Debug,
@@ -121,7 +123,7 @@ public static void ViewCompilerStartCodeGeneration(this ILogger logger, string f
121123
public static void ViewCompilerEndCodeGeneration(this ILogger logger, string filePath, long startTimestamp)
122124
{
123125
// Don't log if logging wasn't enabled at start of request as time will be wildly wrong.
124-
if (startTimestamp != 0)
126+
if (startTimestamp != 0 && logger.IsEnabled(LogLevel.Debug))
125127
{
126128
var currentTimestamp = Stopwatch.GetTimestamp();
127129
var elapsed = new TimeSpan((long)(TimestampToTicks * (currentTimestamp - startTimestamp)));
@@ -192,7 +194,7 @@ public static void TagHelperComponentProcessed(this ILogger logger, string compo
192194
public static void GeneratedCodeToAssemblyCompilationEnd(this ILogger logger, string filePath, long startTimestamp)
193195
{
194196
// Don't log if logging wasn't enabled at start of request as time will be wildly wrong.
195-
if (startTimestamp != 0)
197+
if (startTimestamp != 0 && logger.IsEnabled(LogLevel.Debug))
196198
{
197199
var currentTimestamp = Stopwatch.GetTimestamp();
198200
var elapsed = new TimeSpan((long)(TimestampToTicks * (currentTimestamp - startTimestamp)));

src/Mvc/Mvc.ViewFeatures/src/MvcViewFeaturesLoggerExtensions.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@ static MvcViewFeaturesLoggerExtensions()
4444
_viewComponentExecuting = LoggerMessage.Define<string, string[]>(
4545
LogLevel.Debug,
4646
new EventId(1, "ViewComponentExecuting"),
47-
"Executing view component {ViewComponentName} with arguments ({Arguments}).");
47+
"Executing view component {ViewComponentName} with arguments ({Arguments}).",
48+
skipEnabledCheck: true);
4849

4950
_viewComponentExecuted = LoggerMessage.Define<string, double, string>(
5051
LogLevel.Debug,
5152
new EventId(2, "ViewComponentExecuted"),
5253
"Executed view component {ViewComponentName} in {ElapsedMilliseconds}ms and returned " +
53-
"{ViewComponentResult}");
54+
"{ViewComponentResult}",
55+
skipEnabledCheck: true);
5456

5557
_partialViewResultExecuting = LoggerMessage.Define<string>(
5658
LogLevel.Information,
@@ -136,8 +138,11 @@ public static void ViewComponentExecuting(
136138
ViewComponentContext context,
137139
object[] arguments)
138140
{
139-
var formattedArguments = GetFormattedArguments(arguments);
140-
_viewComponentExecuting(logger, context.ViewComponentDescriptor.DisplayName, formattedArguments, null);
141+
if (logger.IsEnabled(LogLevel.Debug))
142+
{
143+
var formattedArguments = GetFormattedArguments(arguments);
144+
_viewComponentExecuting(logger, context.ViewComponentDescriptor.DisplayName, formattedArguments, null);
145+
}
141146
}
142147

143148
private static string[] GetFormattedArguments(object[] arguments)
@@ -163,12 +168,15 @@ public static void ViewComponentExecuted(
163168
object result)
164169
{
165170
// Don't log if logging wasn't enabled at start of request as time will be wildly wrong.
166-
_viewComponentExecuted(
167-
logger,
168-
context.ViewComponentDescriptor.DisplayName,
169-
timespan.TotalMilliseconds,
170-
Convert.ToString(result, CultureInfo.InvariantCulture),
171-
null);
171+
if (logger.IsEnabled(LogLevel.Debug))
172+
{
173+
_viewComponentExecuted(
174+
logger,
175+
context.ViewComponentDescriptor.DisplayName,
176+
timespan.TotalMilliseconds,
177+
Convert.ToString(result, CultureInfo.InvariantCulture),
178+
null);
179+
}
172180
}
173181

174182
public static void PartialViewFound(

0 commit comments

Comments
 (0)