Added more logging for rewrite/redirect scenarios

This commit is contained in:
Mikael Mengistu 2016-10-10 16:16:56 -07:00 коммит произвёл GitHub
Родитель 9814f3bfbc
Коммит 03b63e2c2a
6 изменённых файлов: 63 добавлений и 16 удалений

Просмотреть файл

@ -1,9 +1,9 @@
<rewrite>
<rules>
<rule name="" stopProcessing="true">
<rule name="Rewrite 20 to 10" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{QUERY_STRING}" pattern="id=20" />
<add input="{QUERY_STRING}" pattern="id=20" />
</conditions>
<action type="Rewrite" url="app?id=10" appendQueryString="false"/>
</rule>

Просмотреть файл

@ -8,28 +8,34 @@ namespace Microsoft.AspNetCore.Rewrite.Logging
{
internal static class RewriteMiddlewareLoggingExtensions
{
private static readonly Action<ILogger, Exception> _requestContinueResults;
private static readonly Action<ILogger, string, Exception> _requestContinueResults;
private static readonly Action<ILogger, string, int, Exception> _requestResponseComplete;
private static readonly Action<ILogger, Exception> _requestStopRules;
private static readonly Action<ILogger, string, Exception> _requestStopRules;
private static readonly Action<ILogger, string, Exception> _urlRewriteDidNotMatchRule;
private static readonly Action<ILogger, string, Exception> _urlRewriteMatchedRule;
private static readonly Action<ILogger, Exception> _modRewriteDidNotMatchRule;
private static readonly Action<ILogger, Exception> _modRewriteMatchedRule;
private static readonly Action<ILogger, Exception> _redirectedToHttps;
private static readonly Action<ILogger, string, Exception> _redirectSummary;
private static readonly Action<ILogger, string, Exception> _rewriteSummary;
static RewriteMiddlewareLoggingExtensions()
{
_requestContinueResults = LoggerMessage.Define(
_requestContinueResults = LoggerMessage.Define<string>(
LogLevel.Debug,
1,
"Request is continuing in applying rules.");
"Request is continuing in applying rules. Current url is {currentUrl}");
_requestResponseComplete = LoggerMessage.Define<string, int>(
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<string>(
LogLevel.Debug,
3,
"Request is done applying rules.");
"Request is done applying rules. Url was rewritten to {rewrittenUrl}");
_urlRewriteDidNotMatchRule = LoggerMessage.Define<string>(
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<string>(
LogLevel.Information,
9,
"Request was redirected to {redirectedUrl}");
_rewriteSummary = LoggerMessage.Define<string>(
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);
}
}
}

Просмотреть файл

@ -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);
}
}
}

Просмотреть файл

@ -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();
}
}
}

Просмотреть файл

@ -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);
}
}
}

Просмотреть файл

@ -24,7 +24,7 @@ namespace Microsoft.AspNetCore.Rewrite
private readonly ILogger _logger;
/// <summary>
/// Creates a new instance of <see cref="RewriteMiddleware"/>
/// Creates a new instance of <see cref="RewriteMiddleware"/>
/// </summary>
/// <param name="next">The delegate representing the next middleware in the request pipeline.</param>
/// <param name="hostingEnvironment">The Hosting Environment.</param>
@ -75,10 +75,11 @@ namespace Microsoft.AspNetCore.Rewrite
foreach (var rule in _options.Rules)
{
rule.ApplyRule(rewriteContext);
var currentUrl = new Lazy<string>(() => 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}");