|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using Microsoft.Extensions.Logging; |
| 6 | +using Microsoft.Extensions.Primitives; |
| 7 | + |
| 8 | +namespace Microsoft.AspNet.StaticFiles |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Defines *all* the logger messages produced by static files |
| 12 | + /// </summary> |
| 13 | + internal static class LoggerExtensions |
| 14 | + { |
| 15 | + private static Action<ILogger, string, Exception> _logMethodNotSupported; |
| 16 | + private static Action<ILogger, string, string, Exception> _logFileServed; |
| 17 | + private static Action<ILogger, string, Exception> _logPathMismatch; |
| 18 | + private static Action<ILogger, string, Exception> _logFileTypeNotSupported; |
| 19 | + private static Action<ILogger, string, Exception> _logFileNotFound; |
| 20 | + private static Action<ILogger, string, Exception> _logPathNotModified; |
| 21 | + private static Action<ILogger, string, Exception> _logPreconditionFailed; |
| 22 | + private static Action<ILogger, int, string, Exception> _logHandled; |
| 23 | + private static Action<ILogger, string, Exception> _logRangeNotSatisfiable; |
| 24 | + private static Action<ILogger, StringValues, string, Exception> _logSendingFileRange; |
| 25 | + private static Action<ILogger, StringValues, string, Exception> _logCopyingFileRange; |
| 26 | + private static Action<ILogger, long, string, string, Exception> _logCopyingBytesToResponse; |
| 27 | + private static Action<ILogger, string, Exception> _logMultipleFileRanges; |
| 28 | + |
| 29 | + static LoggerExtensions() |
| 30 | + { |
| 31 | + _logMethodNotSupported = LoggerMessage.Define<string>( |
| 32 | + logLevel: LogLevel.Verbose, |
| 33 | + eventId: 1, |
| 34 | + formatString: "{Method} requests are not supported"); |
| 35 | + _logFileServed = LoggerMessage.Define<string, string>( |
| 36 | + logLevel: LogLevel.Information, |
| 37 | + eventId: 2, |
| 38 | + formatString: "Sending file. Request path: '{VirtualPath}'. Physical path: '{PhysicalPath}'"); |
| 39 | + _logPathMismatch = LoggerMessage.Define<string>( |
| 40 | + logLevel: LogLevel.Verbose, |
| 41 | + eventId: 3, |
| 42 | + formatString: "The request path {Path} does not match the path filter"); |
| 43 | + _logFileTypeNotSupported = LoggerMessage.Define<string>( |
| 44 | + logLevel: LogLevel.Verbose, |
| 45 | + eventId: 4, |
| 46 | + formatString: "The request path {Path} does not match a supported file type"); |
| 47 | + _logFileNotFound = LoggerMessage.Define<string>( |
| 48 | + logLevel: LogLevel.Verbose, |
| 49 | + eventId: 5, |
| 50 | + formatString: "The request path {Path} does not match an existing file"); |
| 51 | + _logPathNotModified = LoggerMessage.Define<string>( |
| 52 | + logLevel: LogLevel.Information, |
| 53 | + eventId: 6, |
| 54 | + formatString: "The file {Path} was not modified"); |
| 55 | + _logPreconditionFailed = LoggerMessage.Define<string>( |
| 56 | + logLevel: LogLevel.Information, |
| 57 | + eventId: 7, |
| 58 | + formatString: "Precondition for {Path} failed"); |
| 59 | + _logHandled = LoggerMessage.Define<int, string>( |
| 60 | + logLevel: LogLevel.Verbose, |
| 61 | + eventId: 8, |
| 62 | + formatString: "Handled. Status code: {StatusCode} File: {Path}"); |
| 63 | + _logRangeNotSatisfiable = LoggerMessage.Define<string>( |
| 64 | + logLevel: LogLevel.Warning, |
| 65 | + eventId: 9, |
| 66 | + formatString: "Range not satisfiable for {Path}"); |
| 67 | + _logSendingFileRange = LoggerMessage.Define<StringValues, string>( |
| 68 | + logLevel: LogLevel.Information, |
| 69 | + eventId: 10, |
| 70 | + formatString: "Sending {Range} of file {Path}"); |
| 71 | + _logCopyingFileRange = LoggerMessage.Define<StringValues, string>( |
| 72 | + logLevel: LogLevel.Verbose, |
| 73 | + eventId: 11, |
| 74 | + formatString: "Copying {Range} of file {Path} to the response body"); |
| 75 | + _logCopyingBytesToResponse = LoggerMessage.Define<long, string, string>( |
| 76 | + logLevel: LogLevel.Verbose, |
| 77 | + eventId: 12, |
| 78 | + formatString: "Copying bytes {Start}-{End} of file {Path} to response body"); |
| 79 | + _logMultipleFileRanges = LoggerMessage.Define<string>( |
| 80 | + logLevel: LogLevel.Warning, |
| 81 | + eventId: 13, |
| 82 | + formatString: "Multiple ranges are not allowed: '{Ranges}'"); |
| 83 | + } |
| 84 | + |
| 85 | + public static void LogRequestMethodNotSupported(this ILogger logger, string method) |
| 86 | + { |
| 87 | + _logMethodNotSupported(logger, method, null); |
| 88 | + } |
| 89 | + |
| 90 | + public static void LogFileServed(this ILogger logger, string virtualPath, string physicalPath) |
| 91 | + { |
| 92 | + if (string.IsNullOrEmpty(physicalPath)) |
| 93 | + { |
| 94 | + physicalPath = "N/A"; |
| 95 | + } |
| 96 | + _logFileServed(logger, virtualPath, physicalPath, null); |
| 97 | + } |
| 98 | + |
| 99 | + public static void LogPathMismatch(this ILogger logger, string path) |
| 100 | + { |
| 101 | + _logPathMismatch(logger, path, null); |
| 102 | + } |
| 103 | + |
| 104 | + public static void LogFileTypeNotSupported(this ILogger logger, string path) |
| 105 | + { |
| 106 | + _logFileTypeNotSupported(logger, path, null); |
| 107 | + } |
| 108 | + |
| 109 | + public static void LogFileNotFound(this ILogger logger, string path) |
| 110 | + { |
| 111 | + _logFileNotFound(logger, path, null); |
| 112 | + } |
| 113 | + |
| 114 | + public static void LogPathNotModified(this ILogger logger, string path) |
| 115 | + { |
| 116 | + _logPathNotModified(logger, path, null); |
| 117 | + } |
| 118 | + |
| 119 | + public static void LogPreconditionFailed(this ILogger logger, string path) |
| 120 | + { |
| 121 | + _logPreconditionFailed(logger, path, null); |
| 122 | + } |
| 123 | + |
| 124 | + public static void LogHandled(this ILogger logger, int statusCode, string path) |
| 125 | + { |
| 126 | + _logHandled(logger, statusCode, path, null); |
| 127 | + } |
| 128 | + |
| 129 | + public static void LogRangeNotSatisfiable(this ILogger logger, string path) |
| 130 | + { |
| 131 | + _logRangeNotSatisfiable(logger, path, null); |
| 132 | + } |
| 133 | + |
| 134 | + public static void LogSendingFileRange(this ILogger logger, StringValues range, string path) |
| 135 | + { |
| 136 | + _logSendingFileRange(logger, range, path, null); |
| 137 | + } |
| 138 | + |
| 139 | + public static void LogCopyingFileRange(this ILogger logger, StringValues range, string path) |
| 140 | + { |
| 141 | + _logCopyingFileRange(logger, range, path, null); |
| 142 | + } |
| 143 | + |
| 144 | + public static void LogCopyingBytesToResponse(this ILogger logger, long start, long? end, string path) |
| 145 | + { |
| 146 | + _logCopyingBytesToResponse( |
| 147 | + logger, |
| 148 | + start, |
| 149 | + end != null ? end.ToString() : "*", |
| 150 | + path, |
| 151 | + null); |
| 152 | + } |
| 153 | + |
| 154 | + public static void LogMultipleFileRanges(this ILogger logger, string range) |
| 155 | + { |
| 156 | + _logMultipleFileRanges(logger, range, null); |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments