Bläddra i källkod

Add event name in static files (#6456)

James Newton-King 7 år sedan
förälder
incheckning
dd39bd289a

+ 60 - 75
src/Middleware/StaticFiles/src/LoggerExtensions.cs

@@ -12,148 +12,133 @@ namespace Microsoft.AspNetCore.StaticFiles
     /// </summary>
     internal static class LoggerExtensions
     {
-        private static Action<ILogger, string, Exception> _logMethodNotSupported;
-        private static Action<ILogger, string, string, Exception> _logFileServed;
-        private static Action<ILogger, string, Exception> _logPathMismatch;
-        private static Action<ILogger, string, Exception> _logFileTypeNotSupported;
-        private static Action<ILogger, string, Exception> _logFileNotFound;
-        private static Action<ILogger, string, Exception> _logPathNotModified;
-        private static Action<ILogger, string, Exception> _logPreconditionFailed;
-        private static Action<ILogger, int, string, Exception> _logHandled;
-        private static Action<ILogger, string, Exception> _logRangeNotSatisfiable;
-        private static Action<ILogger, StringValues, string, Exception> _logSendingFileRange;
-        private static Action<ILogger, StringValues, string, Exception> _logCopyingFileRange;
-        private static Action<ILogger, long, string, string, Exception> _logCopyingBytesToResponse;
-        private static Action<ILogger, Exception> _logWriteCancelled;
+        private static Action<ILogger, string, Exception> _methodNotSupported;
+        private static Action<ILogger, string, string, Exception> _fileServed;
+        private static Action<ILogger, string, Exception> _pathMismatch;
+        private static Action<ILogger, string, Exception> _fileTypeNotSupported;
+        private static Action<ILogger, string, Exception> _fileNotFound;
+        private static Action<ILogger, string, Exception> _fileNotModified;
+        private static Action<ILogger, string, Exception> _preconditionFailed;
+        private static Action<ILogger, int, string, Exception> _handled;
+        private static Action<ILogger, string, Exception> _rangeNotSatisfiable;
+        private static Action<ILogger, StringValues, string, Exception> _sendingFileRange;
+        private static Action<ILogger, StringValues, string, Exception> _copyingFileRange;
+        private static Action<ILogger, Exception> _writeCancelled;
 
         static LoggerExtensions()
         {
-            _logMethodNotSupported = LoggerMessage.Define<string>(
+            _methodNotSupported = LoggerMessage.Define<string>(
                 logLevel: LogLevel.Debug,
-                eventId: 1,
+                eventId: new EventId(1, "MethodNotSupported"),
                 formatString: "{Method} requests are not supported");
-            _logFileServed = LoggerMessage.Define<string, string>(
+            _fileServed = LoggerMessage.Define<string, string>(
                logLevel: LogLevel.Information,
-               eventId: 2,
+               eventId: new EventId(2, "FileServed"),
                formatString: "Sending file. Request path: '{VirtualPath}'. Physical path: '{PhysicalPath}'");
-            _logPathMismatch = LoggerMessage.Define<string>(
+            _pathMismatch = LoggerMessage.Define<string>(
                 logLevel: LogLevel.Debug,
-                eventId: 3,
+                eventId: new EventId(3, "PathMismatch"),
                 formatString: "The request path {Path} does not match the path filter");
-            _logFileTypeNotSupported = LoggerMessage.Define<string>(
+            _fileTypeNotSupported = LoggerMessage.Define<string>(
                 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<string>(
+            _fileNotFound = LoggerMessage.Define<string>(
                 logLevel: LogLevel.Debug,
-                eventId: 5,
+                eventId: new EventId(5, "FileNotFound"),
                 formatString: "The request path {Path} does not match an existing file");
-            _logPathNotModified = LoggerMessage.Define<string>(
+            _fileNotModified = LoggerMessage.Define<string>(
                 logLevel: LogLevel.Information,
-                eventId: 6,
+                eventId: new EventId(6, "FileNotModified"),
                 formatString: "The file {Path} was not modified");
-            _logPreconditionFailed = LoggerMessage.Define<string>(
+            _preconditionFailed = LoggerMessage.Define<string>(
                 logLevel: LogLevel.Information,
-                eventId: 7,
+                eventId: new EventId(7, "PreconditionFailed"),
                 formatString: "Precondition for {Path} failed");
-            _logHandled = LoggerMessage.Define<int, string>(
+            _handled = LoggerMessage.Define<int, string>(
                 logLevel: LogLevel.Debug,
-                eventId: 8,
+                eventId: new EventId(8, "Handled"),
                 formatString: "Handled. Status code: {StatusCode} File: {Path}");
-            _logRangeNotSatisfiable = LoggerMessage.Define<string>(
+            _rangeNotSatisfiable = LoggerMessage.Define<string>(
                 logLevel: LogLevel.Warning,
-                eventId: 9,
+                eventId: new EventId(9, "RangeNotSatisfiable"),
                 formatString: "Range not satisfiable for {Path}");
-            _logSendingFileRange = LoggerMessage.Define<StringValues, string>(
+            _sendingFileRange = LoggerMessage.Define<StringValues, string>(
                 logLevel: LogLevel.Information,
-                eventId: 10,
+                eventId: new EventId(10, "SendingFileRange"),
                 formatString: "Sending {Range} of file {Path}");
-            _logCopyingFileRange = LoggerMessage.Define<StringValues, string>(
+            _copyingFileRange = LoggerMessage.Define<StringValues, string>(
                 logLevel: LogLevel.Debug,
-                eventId: 11,
+                eventId: new EventId(11, "CopyingFileRange"),
                 formatString: "Copying {Range} of file {Path} to the response body");
-            _logCopyingBytesToResponse = LoggerMessage.Define<long, string, string>(
+            _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);
-        }
-
-        public static void LogPathMismatch(this ILogger logger, string path)
-        {
-            _logPathMismatch(logger, path, null);
+            _fileServed(logger, virtualPath, physicalPath, null);
         }
 
-        public static void LogFileTypeNotSupported(this ILogger logger, string path)
+        public static void PathMismatch(this ILogger logger, string path)
         {
-            _logFileTypeNotSupported(logger, path, null);
+            _pathMismatch(logger, path, null);
         }
 
-        public static void LogFileNotFound(this ILogger logger, string path)
+        public static void FileTypeNotSupported(this ILogger logger, string path)
         {
-            _logFileNotFound(logger, path, null);
+            _fileTypeNotSupported(logger, path, null);
         }
 
-        public static void LogPathNotModified(this ILogger logger, string path)
+        public static void FileNotFound(this ILogger logger, string path)
         {
-            _logPathNotModified(logger, path, null);
+            _fileNotFound(logger, path, null);
         }
 
-        public static void LogPreconditionFailed(this ILogger logger, string path)
+        public static void FileNotModified(this ILogger logger, string path)
         {
-            _logPreconditionFailed(logger, path, null);
+            _fileNotModified(logger, path, null);
         }
 
-        public static void LogHandled(this ILogger logger, int statusCode, string path)
+        public static void PreconditionFailed(this ILogger logger, string path)
         {
-            _logHandled(logger, statusCode, path, null);
+            _preconditionFailed(logger, path, null);
         }
 
-        public static void LogRangeNotSatisfiable(this ILogger logger, string path)
+        public static void Handled(this ILogger logger, int statusCode, string path)
         {
-            _logRangeNotSatisfiable(logger, path, null);
+            _handled(logger, statusCode, path, null);
         }
 
-        public static void LogSendingFileRange(this ILogger logger, StringValues range, string path)
+        public static void RangeNotSatisfiable(this ILogger logger, string path)
         {
-            _logSendingFileRange(logger, range, path, null);
+            _rangeNotSatisfiable(logger, path, null);
         }
 
-        public static void LogCopyingFileRange(this ILogger logger, StringValues range, string path)
+        public static void SendingFileRange(this ILogger logger, StringValues range, string path)
         {
-            _logCopyingFileRange(logger, range, path, null);
+            _sendingFileRange(logger, range, path, null);
         }
 
-        public static void LogCopyingBytesToResponse(this ILogger logger, long start, long? end, string path)
+        public static void CopyingFileRange(this ILogger logger, StringValues range, string path)
         {
-            _logCopyingBytesToResponse(
-                logger,
-                start,
-                end != null ? end.ToString() : "*",
-                path,
-                null);
+            _copyingFileRange(logger, range, path, null);
         }
 
-        public static void LogWriteCancelled(this ILogger logger, Exception ex)
+        public static void WriteCancelled(this ILogger logger, Exception ex)
         {
-            _logWriteCancelled(logger, ex);
+            _writeCancelled(logger, ex);
         }
     }
 }

+ 6 - 6
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<IHttpSendFileFeature>();
             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();

+ 7 - 7
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;