Log type of ObjectResult in ObjectResultExecutor (#21425)

* Log type of ObjectResult in ObjectResultExecutor

* Log only class name of FileResult, ObjectResult types
This commit is contained in:
Andrew J Said 2020-05-07 20:55:57 +01:00 committed by GitHub
parent 98d5b1bbfd
commit b328f501fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 13 deletions

View File

@ -155,7 +155,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure
return Task.CompletedTask; return Task.CompletedTask;
} }
Logger.ObjectResultExecuting(value); Logger.ObjectResultExecuting(result, value);
result.OnFormatting(context); result.OnFormatting(context);
return selectedFormatter.WriteAsync(formatterContext); return selectedFormatter.WriteAsync(formatterContext);

View File

@ -54,8 +54,8 @@ namespace Microsoft.AspNetCore.Mvc
private static readonly Action<ILogger, string, Exception> _ambiguousActions; private static readonly Action<ILogger, string, Exception> _ambiguousActions;
private static readonly Action<ILogger, string, string, IActionConstraint, Exception> _constraintMismatch; private static readonly Action<ILogger, string, string, IActionConstraint, Exception> _constraintMismatch;
private static readonly Action<ILogger, FileResult, string, string, Exception> _executingFileResult; private static readonly Action<ILogger, string, string, string, Exception> _executingFileResult;
private static readonly Action<ILogger, FileResult, string, Exception> _executingFileResultWithNoFileName; private static readonly Action<ILogger, string, string, Exception> _executingFileResultWithNoFileName;
private static readonly Action<ILogger, Exception> _notEnabledForRangeProcessing; private static readonly Action<ILogger, Exception> _notEnabledForRangeProcessing;
private static readonly Action<ILogger, Exception> _writingRangeToBody; private static readonly Action<ILogger, Exception> _writingRangeToBody;
private static readonly Action<ILogger, object, Exception> _authorizationFailure; private static readonly Action<ILogger, object, Exception> _authorizationFailure;
@ -72,7 +72,7 @@ namespace Microsoft.AspNetCore.Mvc
private static readonly Action<ILogger, string, Exception> _localRedirectResultExecuting; private static readonly Action<ILogger, string, Exception> _localRedirectResultExecuting;
private static readonly Action<ILogger, string, Exception> _objectResultExecuting; private static readonly Action<ILogger, string, string, Exception> _objectResultExecuting;
private static readonly Action<ILogger, IEnumerable<string>, Exception> _noFormatter; private static readonly Action<ILogger, IEnumerable<string>, Exception> _noFormatter;
private static readonly Action<ILogger, IOutputFormatter, string, Exception> _formatterSelected; private static readonly Action<ILogger, IOutputFormatter, string, Exception> _formatterSelected;
private static readonly Action<ILogger, string, Exception> _skippedContentNegotiation; private static readonly Action<ILogger, string, Exception> _skippedContentNegotiation;
@ -250,12 +250,12 @@ namespace Microsoft.AspNetCore.Mvc
new EventId(2, "ConstraintMismatch"), new EventId(2, "ConstraintMismatch"),
"Action '{ActionName}' with id '{ActionId}' did not match the constraint '{ActionConstraint}'"); "Action '{ActionName}' with id '{ActionId}' did not match the constraint '{ActionConstraint}'");
_executingFileResult = LoggerMessage.Define<FileResult, string, string>( _executingFileResult = LoggerMessage.Define<string, string, string>(
LogLevel.Information, LogLevel.Information,
new EventId(1, "ExecutingFileResult"), new EventId(1, "ExecutingFileResult"),
"Executing {FileResultType}, sending file '{FileDownloadPath}' with download name '{FileDownloadName}' ..."); "Executing {FileResultType}, sending file '{FileDownloadPath}' with download name '{FileDownloadName}' ...");
_executingFileResultWithNoFileName = LoggerMessage.Define<FileResult, string>( _executingFileResultWithNoFileName = LoggerMessage.Define<string, string>(
LogLevel.Information, LogLevel.Information,
new EventId(2, "ExecutingFileResultWithNoFileName"), new EventId(2, "ExecutingFileResultWithNoFileName"),
"Executing {FileResultType}, sending file with download name '{FileDownloadName}' ..."); "Executing {FileResultType}, sending file with download name '{FileDownloadName}' ...");
@ -315,10 +315,10 @@ namespace Microsoft.AspNetCore.Mvc
new EventId(1, "NoFormatter"), new EventId(1, "NoFormatter"),
"No output formatter was found for content types '{ContentTypes}' to write the response."); "No output formatter was found for content types '{ContentTypes}' to write the response.");
_objectResultExecuting = LoggerMessage.Define<string>( _objectResultExecuting = LoggerMessage.Define<string, string>(
LogLevel.Information, LogLevel.Information,
new EventId(1, "ObjectResultExecuting"), new EventId(1, "ObjectResultExecuting"),
"Executing ObjectResult, writing value of type '{Type}'."); "Executing {ObjectResultType}, writing value of type '{Type}'.");
_formatterSelected = LoggerMessage.Define<IOutputFormatter, string>( _formatterSelected = LoggerMessage.Define<IOutputFormatter, string>(
LogLevel.Debug, LogLevel.Debug,
@ -933,12 +933,20 @@ namespace Microsoft.AspNetCore.Mvc
public static void ExecutingFileResult(this ILogger logger, FileResult fileResult) public static void ExecutingFileResult(this ILogger logger, FileResult fileResult)
{ {
_executingFileResultWithNoFileName(logger, fileResult, fileResult.FileDownloadName, null); if (logger.IsEnabled(LogLevel.Information))
{
var fileResultType = fileResult.GetType().Name;
_executingFileResultWithNoFileName(logger, fileResultType, fileResult.FileDownloadName, null);
}
} }
public static void ExecutingFileResult(this ILogger logger, FileResult fileResult, string fileName) public static void ExecutingFileResult(this ILogger logger, FileResult fileResult, string fileName)
{ {
_executingFileResult(logger, fileResult, fileName, fileResult.FileDownloadName, null); if (logger.IsEnabled(LogLevel.Information))
{
var fileResultType = fileResult.GetType().Name;
_executingFileResult(logger, fileResultType, fileName, fileResult.FileDownloadName, null);
}
} }
public static void NotEnabledForRangeProcessing(this ILogger logger) public static void NotEnabledForRangeProcessing(this ILogger logger)
@ -1017,12 +1025,13 @@ namespace Microsoft.AspNetCore.Mvc
_localRedirectResultExecuting(logger, destination, null); _localRedirectResultExecuting(logger, destination, null);
} }
public static void ObjectResultExecuting(this ILogger logger, object value) public static void ObjectResultExecuting(this ILogger logger, ObjectResult result, object value)
{ {
if (logger.IsEnabled(LogLevel.Information)) if (logger.IsEnabled(LogLevel.Information))
{ {
var type = value == null ? "null" : value.GetType().FullName; var objectResultType = result.GetType().Name;
_objectResultExecuting(logger, type, null); var valueType = value == null ? "null" : value.GetType().FullName;
_objectResultExecuting(logger, objectResultType, valueType, null);
} }
} }