// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Primitives; namespace Microsoft.AspNetCore.StaticFiles { /// /// Defines *all* the logger messages produced by static files /// 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 _logMultipleFileRanges; static LoggerExtensions() { _logMethodNotSupported = LoggerMessage.Define( logLevel: LogLevel.Debug, eventId: 1, formatString: "{Method} requests are not supported"); _logFileServed = LoggerMessage.Define( logLevel: LogLevel.Information, eventId: 2, formatString: "Sending file. Request path: '{VirtualPath}'. Physical path: '{PhysicalPath}'"); _logPathMismatch = LoggerMessage.Define( logLevel: LogLevel.Debug, eventId: 3, formatString: "The request path {Path} does not match the path filter"); _logFileTypeNotSupported = LoggerMessage.Define( logLevel: LogLevel.Debug, eventId: 4, formatString: "The request path {Path} does not match a supported file type"); _logFileNotFound = LoggerMessage.Define( logLevel: LogLevel.Debug, eventId: 5, formatString: "The request path {Path} does not match an existing file"); _logPathNotModified = LoggerMessage.Define( logLevel: LogLevel.Information, eventId: 6, formatString: "The file {Path} was not modified"); _logPreconditionFailed = LoggerMessage.Define( logLevel: LogLevel.Information, eventId: 7, formatString: "Precondition for {Path} failed"); _logHandled = LoggerMessage.Define( logLevel: LogLevel.Debug, eventId: 8, formatString: "Handled. Status code: {StatusCode} File: {Path}"); _logRangeNotSatisfiable = LoggerMessage.Define( logLevel: LogLevel.Warning, eventId: 9, formatString: "Range not satisfiable for {Path}"); _logSendingFileRange = LoggerMessage.Define( logLevel: LogLevel.Information, eventId: 10, formatString: "Sending {Range} of file {Path}"); _logCopyingFileRange = LoggerMessage.Define( logLevel: LogLevel.Debug, eventId: 11, formatString: "Copying {Range} of file {Path} to the response body"); _logCopyingBytesToResponse = LoggerMessage.Define( logLevel: LogLevel.Debug, eventId: 12, formatString: "Copying bytes {Start}-{End} of file {Path} to response body"); _logMultipleFileRanges = LoggerMessage.Define( logLevel: LogLevel.Warning, eventId: 13, formatString: "Multiple ranges are not allowed: '{Ranges}'"); } public static void LogRequestMethodNotSupported(this ILogger logger, string method) { _logMethodNotSupported(logger, method, null); } public static void LogFileServed(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); } public static void LogFileTypeNotSupported(this ILogger logger, string path) { _logFileTypeNotSupported(logger, path, null); } public static void LogFileNotFound(this ILogger logger, string path) { _logFileNotFound(logger, path, null); } public static void LogPathNotModified(this ILogger logger, string path) { _logPathNotModified(logger, path, null); } public static void LogPreconditionFailed(this ILogger logger, string path) { _logPreconditionFailed(logger, path, null); } public static void LogHandled(this ILogger logger, int statusCode, string path) { _logHandled(logger, statusCode, path, null); } public static void LogRangeNotSatisfiable(this ILogger logger, string path) { _logRangeNotSatisfiable(logger, path, null); } public static void LogSendingFileRange(this ILogger logger, StringValues range, string path) { _logSendingFileRange(logger, range, path, null); } public static void LogCopyingFileRange(this ILogger logger, StringValues range, string path) { _logCopyingFileRange(logger, range, path, null); } public static void LogCopyingBytesToResponse(this ILogger logger, long start, long? end, string path) { _logCopyingBytesToResponse( logger, start, end != null ? end.ToString() : "*", path, null); } public static void LogMultipleFileRanges(this ILogger logger, string range) { _logMultipleFileRanges(logger, range, null); } } }