From 03b63e2c2a5ec27deef4be692ff2119188b67d73 Mon Sep 17 00:00:00 2001 From: Mikael Mengistu Date: Mon, 10 Oct 2016 16:16:56 -0700 Subject: [PATCH] Added more logging for rewrite/redirect scenarios --- samples/RewriteSample/UrlRewrite.xml | 4 +- .../RewriteMiddlewareLoggingExtensions.cs | 59 +++++++++++++++---- .../Internal/RedirectRule.cs | 4 ++ .../Internal/RedirectToHttpsRule.cs | 2 + .../Internal/RewriteRule.cs | 3 + .../RewriteMiddleware.cs | 7 ++- 6 files changed, 63 insertions(+), 16 deletions(-) diff --git a/samples/RewriteSample/UrlRewrite.xml b/samples/RewriteSample/UrlRewrite.xml index fee8310..7ace7ba 100644 --- a/samples/RewriteSample/UrlRewrite.xml +++ b/samples/RewriteSample/UrlRewrite.xml @@ -1,9 +1,9 @@  - + - + diff --git a/src/Microsoft.AspNetCore.Rewrite/Extensions/RewriteMiddlewareLoggingExtensions.cs b/src/Microsoft.AspNetCore.Rewrite/Extensions/RewriteMiddlewareLoggingExtensions.cs index 0d32fe6..296a38f 100644 --- a/src/Microsoft.AspNetCore.Rewrite/Extensions/RewriteMiddlewareLoggingExtensions.cs +++ b/src/Microsoft.AspNetCore.Rewrite/Extensions/RewriteMiddlewareLoggingExtensions.cs @@ -8,28 +8,34 @@ namespace Microsoft.AspNetCore.Rewrite.Logging { internal static class RewriteMiddlewareLoggingExtensions { - private static readonly Action _requestContinueResults; + private static readonly Action _requestContinueResults; private static readonly Action _requestResponseComplete; - private static readonly Action _requestStopRules; + private static readonly Action _requestStopRules; private static readonly Action _urlRewriteDidNotMatchRule; private static readonly Action _urlRewriteMatchedRule; private static readonly Action _modRewriteDidNotMatchRule; private static readonly Action _modRewriteMatchedRule; + private static readonly Action _redirectedToHttps; + private static readonly Action _redirectSummary; + private static readonly Action _rewriteSummary; static RewriteMiddlewareLoggingExtensions() { - _requestContinueResults = LoggerMessage.Define( + _requestContinueResults = LoggerMessage.Define( LogLevel.Debug, 1, - "Request is continuing in applying rules."); + "Request is continuing in applying rules. Current url is {currentUrl}"); + _requestResponseComplete = LoggerMessage.Define( LogLevel.Debug, 2, - "Request is done processing, Location header '{Location}' with status code '{StatusCode}'."); - _requestStopRules = LoggerMessage.Define( + "Request is done processing. Location header '{Location}' with status code '{StatusCode}'."); + + _requestStopRules = LoggerMessage.Define( LogLevel.Debug, 3, - "Request is done applying rules."); + "Request is done applying rules. Url was rewritten to {rewrittenUrl}"); + _urlRewriteDidNotMatchRule = LoggerMessage.Define( LogLevel.Debug, 4, @@ -49,11 +55,26 @@ namespace Microsoft.AspNetCore.Rewrite.Logging LogLevel.Debug, 7, "Request matched current ModRewriteRule."); + + _redirectedToHttps = LoggerMessage.Define( + LogLevel.Information, + 8, + "Request redirected to HTTPS"); + + _redirectSummary = LoggerMessage.Define( + LogLevel.Information, + 9, + "Request was redirected to {redirectedUrl}"); + + _rewriteSummary = LoggerMessage.Define( + LogLevel.Information, + 10, + "Request was rewritten to {rewrittenUrl}"); } - public static void RewriteMiddlewareRequestContinueResults(this ILogger logger) + public static void RewriteMiddlewareRequestContinueResults(this ILogger logger, string currentUrl) { - _requestContinueResults(logger, null); + _requestContinueResults(logger, currentUrl, null); } public static void RewriteMiddlewareRequestResponseComplete(this ILogger logger, string location, int statusCode) @@ -61,9 +82,9 @@ namespace Microsoft.AspNetCore.Rewrite.Logging _requestResponseComplete(logger, location, statusCode, null); } - public static void RewriteMiddlewareRequestStopRules(this ILogger logger) + public static void RewriteMiddlewareRequestStopRules(this ILogger logger, string rewrittenUrl) { - _requestStopRules(logger, null); + _requestStopRules(logger, rewrittenUrl, null); } public static void UrlRewriteDidNotMatchRule(this ILogger logger, string name) @@ -80,9 +101,25 @@ namespace Microsoft.AspNetCore.Rewrite.Logging { _modRewriteDidNotMatchRule(logger, null); } + public static void ModRewriteMatchedRule(this ILogger logger) { _modRewriteMatchedRule(logger, null); } + + public static void RedirectedToHttps(this ILogger logger) + { + _redirectedToHttps(logger, null); + } + + public static void RedirectedSummary(this ILogger logger, string redirectedUrl) + { + _redirectSummary(logger, redirectedUrl, null); + } + + public static void RewriteSummary(this ILogger logger, string rewrittenUrl) + { + _rewriteSummary(logger, rewrittenUrl, null); + } } } diff --git a/src/Microsoft.AspNetCore.Rewrite/Internal/RedirectRule.cs b/src/Microsoft.AspNetCore.Rewrite/Internal/RedirectRule.cs index 1198f27..4f63bf6 100644 --- a/src/Microsoft.AspNetCore.Rewrite/Internal/RedirectRule.cs +++ b/src/Microsoft.AspNetCore.Rewrite/Internal/RedirectRule.cs @@ -5,6 +5,7 @@ using System; using System.Text.RegularExpressions; using Microsoft.AspNetCore.Http; using Microsoft.Net.Http.Headers; +using Microsoft.AspNetCore.Rewrite.Logging; namespace Microsoft.AspNetCore.Rewrite.Internal { @@ -46,6 +47,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal initMatchResults = InitialMatch.Match(path.ToString().Substring(1)); } + if (initMatchResults.Success) { var newPath = initMatchResults.Result(Replacement); @@ -78,6 +80,8 @@ namespace Microsoft.AspNetCore.Rewrite.Internal { response.Headers[HeaderNames.Location] = pathBase + newPath; } + + context.Logger?.RedirectedSummary(newPath); } } } diff --git a/src/Microsoft.AspNetCore.Rewrite/Internal/RedirectToHttpsRule.cs b/src/Microsoft.AspNetCore.Rewrite/Internal/RedirectToHttpsRule.cs index 6127785..6fb1fbf 100644 --- a/src/Microsoft.AspNetCore.Rewrite/Internal/RedirectToHttpsRule.cs +++ b/src/Microsoft.AspNetCore.Rewrite/Internal/RedirectToHttpsRule.cs @@ -3,6 +3,7 @@ using System.Text; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Rewrite.Logging; namespace Microsoft.AspNetCore.Rewrite.Internal { @@ -32,6 +33,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal var newUrl = new StringBuilder().Append("https://").Append(host).Append(req.PathBase).Append(req.Path).Append(req.QueryString); context.HttpContext.Response.Redirect(newUrl.ToString()); context.Result = RuleResult.EndResponse; + context.Logger?.RedirectedToHttps(); } } } diff --git a/src/Microsoft.AspNetCore.Rewrite/Internal/RewriteRule.cs b/src/Microsoft.AspNetCore.Rewrite/Internal/RewriteRule.cs index d6452e8..d9e0050 100644 --- a/src/Microsoft.AspNetCore.Rewrite/Internal/RewriteRule.cs +++ b/src/Microsoft.AspNetCore.Rewrite/Internal/RewriteRule.cs @@ -5,6 +5,7 @@ using System; using System.Text.RegularExpressions; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Extensions; +using Microsoft.AspNetCore.Rewrite.Logging; namespace Microsoft.AspNetCore.Rewrite.Internal { @@ -103,6 +104,8 @@ namespace Microsoft.AspNetCore.Rewrite.Internal } } } + + context.Logger?.RewriteSummary(result); } } } diff --git a/src/Microsoft.AspNetCore.Rewrite/RewriteMiddleware.cs b/src/Microsoft.AspNetCore.Rewrite/RewriteMiddleware.cs index 58cd935..953e589 100644 --- a/src/Microsoft.AspNetCore.Rewrite/RewriteMiddleware.cs +++ b/src/Microsoft.AspNetCore.Rewrite/RewriteMiddleware.cs @@ -24,7 +24,7 @@ namespace Microsoft.AspNetCore.Rewrite private readonly ILogger _logger; /// - /// Creates a new instance of + /// Creates a new instance of /// /// The delegate representing the next middleware in the request pipeline. /// The Hosting Environment. @@ -75,10 +75,11 @@ namespace Microsoft.AspNetCore.Rewrite foreach (var rule in _options.Rules) { rule.ApplyRule(rewriteContext); + var currentUrl = new Lazy(() => context.Request.Path + context.Request.QueryString); switch (rewriteContext.Result) { case RuleResult.ContinueRules: - _logger.RewriteMiddlewareRequestContinueResults(); + _logger.RewriteMiddlewareRequestContinueResults(currentUrl.Value); break; case RuleResult.EndResponse: _logger.RewriteMiddlewareRequestResponseComplete( @@ -86,7 +87,7 @@ namespace Microsoft.AspNetCore.Rewrite context.Response.StatusCode); return TaskCache.CompletedTask; case RuleResult.SkipRemainingRules: - _logger.RewriteMiddlewareRequestStopRules(); + _logger.RewriteMiddlewareRequestStopRules(currentUrl.Value); return _next(context); default: throw new ArgumentOutOfRangeException($"Invalid rule termination {rewriteContext.Result}");