diff --git a/src/Middleware/StaticFiles/src/LoggerExtensions.cs b/src/Middleware/StaticFiles/src/LoggerExtensions.cs index 7fa6e35083..56afbeb5eb 100644 --- a/src/Middleware/StaticFiles/src/LoggerExtensions.cs +++ b/src/Middleware/StaticFiles/src/LoggerExtensions.cs @@ -12,148 +12,133 @@ namespace Microsoft.AspNetCore.StaticFiles /// internal static class LoggerExtensions { - private static Action _logMethodNotSupported; - private static Action _logFileServed; - private static Action _logPathMismatch; - private static Action _logFileTypeNotSupported; - private static Action _logFileNotFound; - private static Action _logPathNotModified; - private static Action _logPreconditionFailed; - private static Action _logHandled; - private static Action _logRangeNotSatisfiable; - private static Action _logSendingFileRange; - private static Action _logCopyingFileRange; - private static Action _logCopyingBytesToResponse; - private static Action _logWriteCancelled; + private static Action _methodNotSupported; + private static Action _fileServed; + private static Action _pathMismatch; + private static Action _fileTypeNotSupported; + private static Action _fileNotFound; + private static Action _fileNotModified; + private static Action _preconditionFailed; + private static Action _handled; + private static Action _rangeNotSatisfiable; + private static Action _sendingFileRange; + private static Action _copyingFileRange; + private static Action _writeCancelled; static LoggerExtensions() { - _logMethodNotSupported = LoggerMessage.Define( + _methodNotSupported = LoggerMessage.Define( logLevel: LogLevel.Debug, - eventId: 1, + eventId: new EventId(1, "MethodNotSupported"), formatString: "{Method} requests are not supported"); - _logFileServed = LoggerMessage.Define( + _fileServed = LoggerMessage.Define( logLevel: LogLevel.Information, - eventId: 2, + eventId: new EventId(2, "FileServed"), formatString: "Sending file. Request path: '{VirtualPath}'. Physical path: '{PhysicalPath}'"); - _logPathMismatch = LoggerMessage.Define( + _pathMismatch = LoggerMessage.Define( logLevel: LogLevel.Debug, - eventId: 3, + eventId: new EventId(3, "PathMismatch"), formatString: "The request path {Path} does not match the path filter"); - _logFileTypeNotSupported = LoggerMessage.Define( + _fileTypeNotSupported = LoggerMessage.Define( logLevel: LogLevel.Debug, - eventId: 4, + eventId: new EventId(4, "FileTypeNotSupported"), formatString: "The request path {Path} does not match a supported file type"); - _logFileNotFound = LoggerMessage.Define( + _fileNotFound = LoggerMessage.Define( logLevel: LogLevel.Debug, - eventId: 5, + eventId: new EventId(5, "FileNotFound"), formatString: "The request path {Path} does not match an existing file"); - _logPathNotModified = LoggerMessage.Define( + _fileNotModified = LoggerMessage.Define( logLevel: LogLevel.Information, - eventId: 6, + eventId: new EventId(6, "FileNotModified"), formatString: "The file {Path} was not modified"); - _logPreconditionFailed = LoggerMessage.Define( + _preconditionFailed = LoggerMessage.Define( logLevel: LogLevel.Information, - eventId: 7, + eventId: new EventId(7, "PreconditionFailed"), formatString: "Precondition for {Path} failed"); - _logHandled = LoggerMessage.Define( + _handled = LoggerMessage.Define( logLevel: LogLevel.Debug, - eventId: 8, + eventId: new EventId(8, "Handled"), formatString: "Handled. Status code: {StatusCode} File: {Path}"); - _logRangeNotSatisfiable = LoggerMessage.Define( + _rangeNotSatisfiable = LoggerMessage.Define( logLevel: LogLevel.Warning, - eventId: 9, + eventId: new EventId(9, "RangeNotSatisfiable"), formatString: "Range not satisfiable for {Path}"); - _logSendingFileRange = LoggerMessage.Define( + _sendingFileRange = LoggerMessage.Define( logLevel: LogLevel.Information, - eventId: 10, + eventId: new EventId(10, "SendingFileRange"), formatString: "Sending {Range} of file {Path}"); - _logCopyingFileRange = LoggerMessage.Define( + _copyingFileRange = LoggerMessage.Define( logLevel: LogLevel.Debug, - eventId: 11, + eventId: new EventId(11, "CopyingFileRange"), formatString: "Copying {Range} of file {Path} to the response body"); - _logCopyingBytesToResponse = LoggerMessage.Define( + _writeCancelled = LoggerMessage.Define( logLevel: LogLevel.Debug, - eventId: 12, - formatString: "Copying bytes {Start}-{End} of file {Path} to response body"); - _logWriteCancelled = LoggerMessage.Define( - logLevel: LogLevel.Debug, - eventId: 14, + eventId: new EventId(14, "WriteCancelled"), formatString: "The file transmission was cancelled"); } - public static void LogRequestMethodNotSupported(this ILogger logger, string method) + public static void RequestMethodNotSupported(this ILogger logger, string method) { - _logMethodNotSupported(logger, method, null); + _methodNotSupported(logger, method, null); } - public static void LogFileServed(this ILogger logger, string virtualPath, string physicalPath) + public static void FileServed(this ILogger logger, string virtualPath, string physicalPath) { if (string.IsNullOrEmpty(physicalPath)) { physicalPath = "N/A"; } - _logFileServed(logger, virtualPath, physicalPath, null); + _fileServed(logger, virtualPath, physicalPath, null); } - public static void LogPathMismatch(this ILogger logger, string path) + public static void PathMismatch(this ILogger logger, string path) { - _logPathMismatch(logger, path, null); + _pathMismatch(logger, path, null); } - public static void LogFileTypeNotSupported(this ILogger logger, string path) + public static void FileTypeNotSupported(this ILogger logger, string path) { - _logFileTypeNotSupported(logger, path, null); + _fileTypeNotSupported(logger, path, null); } - public static void LogFileNotFound(this ILogger logger, string path) + public static void FileNotFound(this ILogger logger, string path) { - _logFileNotFound(logger, path, null); + _fileNotFound(logger, path, null); } - public static void LogPathNotModified(this ILogger logger, string path) + public static void FileNotModified(this ILogger logger, string path) { - _logPathNotModified(logger, path, null); + _fileNotModified(logger, path, null); } - public static void LogPreconditionFailed(this ILogger logger, string path) + public static void PreconditionFailed(this ILogger logger, string path) { - _logPreconditionFailed(logger, path, null); + _preconditionFailed(logger, path, null); } - public static void LogHandled(this ILogger logger, int statusCode, string path) + public static void Handled(this ILogger logger, int statusCode, string path) { - _logHandled(logger, statusCode, path, null); + _handled(logger, statusCode, path, null); } - public static void LogRangeNotSatisfiable(this ILogger logger, string path) + public static void RangeNotSatisfiable(this ILogger logger, string path) { - _logRangeNotSatisfiable(logger, path, null); + _rangeNotSatisfiable(logger, path, null); } - public static void LogSendingFileRange(this ILogger logger, StringValues range, string path) + public static void SendingFileRange(this ILogger logger, StringValues range, string path) { - _logSendingFileRange(logger, range, path, null); + _sendingFileRange(logger, range, path, null); } - public static void LogCopyingFileRange(this ILogger logger, StringValues range, string path) + public static void CopyingFileRange(this ILogger logger, StringValues range, string path) { - _logCopyingFileRange(logger, range, path, null); + _copyingFileRange(logger, range, path, null); } - public static void LogCopyingBytesToResponse(this ILogger logger, long start, long? end, string path) + public static void WriteCancelled(this ILogger logger, Exception ex) { - _logCopyingBytesToResponse( - logger, - start, - end != null ? end.ToString() : "*", - path, - null); - } - - public static void LogWriteCancelled(this ILogger logger, Exception ex) - { - _logWriteCancelled(logger, ex); + _writeCancelled(logger, ex); } } } diff --git a/src/Middleware/StaticFiles/src/StaticFileContext.cs b/src/Middleware/StaticFiles/src/StaticFileContext.cs index 6bf8c60f34..d6f0d72779 100644 --- a/src/Middleware/StaticFiles/src/StaticFileContext.cs +++ b/src/Middleware/StaticFiles/src/StaticFileContext.cs @@ -309,7 +309,7 @@ namespace Microsoft.AspNetCore.StaticFiles { ApplyResponseHeaders(statusCode); - _logger.LogHandled(statusCode, SubPath); + _logger.Handled(statusCode, SubPath); return Task.CompletedTask; } @@ -335,7 +335,7 @@ namespace Microsoft.AspNetCore.StaticFiles } catch (OperationCanceledException ex) { - _logger.LogWriteCancelled(ex); + _logger.WriteCancelled(ex); // Don't throw this exception, it's most likely caused by the client disconnecting. // However, if it was cancelled for any other reason we need to prevent empty responses. _context.Abort(); @@ -353,7 +353,7 @@ namespace Microsoft.AspNetCore.StaticFiles _responseHeaders.ContentRange = new ContentRangeHeaderValue(_length); ApplyResponseHeaders(Constants.Status416RangeNotSatisfiable); - _logger.LogRangeNotSatisfiable(SubPath); + _logger.RangeNotSatisfiable(SubPath); return; } @@ -365,7 +365,7 @@ namespace Microsoft.AspNetCore.StaticFiles var sendFile = _context.Features.Get(); if (sendFile != null && !string.IsNullOrEmpty(physicalPath)) { - _logger.LogSendingFileRange(_response.Headers[HeaderNames.ContentRange], physicalPath); + _logger.SendingFileRange(_response.Headers[HeaderNames.ContentRange], physicalPath); // We don't need to directly cancel this, if the client disconnects it will fail silently. await sendFile.SendFileAsync(physicalPath, start, length, CancellationToken.None); return; @@ -376,13 +376,13 @@ namespace Microsoft.AspNetCore.StaticFiles using (var readStream = _fileInfo.CreateReadStream()) { readStream.Seek(start, SeekOrigin.Begin); // TODO: What if !CanSeek? - _logger.LogCopyingFileRange(_response.Headers[HeaderNames.ContentRange], SubPath); + _logger.CopyingFileRange(_response.Headers[HeaderNames.ContentRange], SubPath); await StreamCopyOperation.CopyToAsync(readStream, _response.Body, length, _context.RequestAborted); } } catch (OperationCanceledException ex) { - _logger.LogWriteCancelled(ex); + _logger.WriteCancelled(ex); // Don't throw this exception, it's most likely caused by the client disconnecting. // However, if it was cancelled for any other reason we need to prevent empty responses. _context.Abort(); diff --git a/src/Middleware/StaticFiles/src/StaticFileMiddleware.cs b/src/Middleware/StaticFiles/src/StaticFileMiddleware.cs index 46594fc35d..f35bf453f1 100644 --- a/src/Middleware/StaticFiles/src/StaticFileMiddleware.cs +++ b/src/Middleware/StaticFiles/src/StaticFileMiddleware.cs @@ -74,19 +74,19 @@ namespace Microsoft.AspNetCore.StaticFiles if (!fileContext.ValidateMethod()) { - _logger.LogRequestMethodNotSupported(context.Request.Method); + _logger.RequestMethodNotSupported(context.Request.Method); } else if (!fileContext.ValidatePath()) { - _logger.LogPathMismatch(fileContext.SubPath); + _logger.PathMismatch(fileContext.SubPath); } else if (!fileContext.LookupContentType()) { - _logger.LogFileTypeNotSupported(fileContext.SubPath); + _logger.FileTypeNotSupported(fileContext.SubPath); } else if (!fileContext.LookupFileInfo()) { - _logger.LogFileNotFound(fileContext.SubPath); + _logger.FileNotFound(fileContext.SubPath); } else { @@ -111,7 +111,7 @@ namespace Microsoft.AspNetCore.StaticFiles } await fileContext.SendAsync(); - _logger.LogFileServed(fileContext.SubPath, fileContext.PhysicalPath); + _logger.FileServed(fileContext.SubPath, fileContext.PhysicalPath); return; } catch (FileNotFoundException) @@ -120,12 +120,12 @@ namespace Microsoft.AspNetCore.StaticFiles } break; case StaticFileContext.PreconditionState.NotModified: - _logger.LogPathNotModified(fileContext.SubPath); + _logger.FileNotModified(fileContext.SubPath); await fileContext.SendStatusAsync(Constants.Status304NotModified); return; case StaticFileContext.PreconditionState.PreconditionFailed: - _logger.LogPreconditionFailed(fileContext.SubPath); + _logger.PreconditionFailed(fileContext.SubPath); await fileContext.SendStatusAsync(Constants.Status412PreconditionFailed); return;