Added more logging for rewrite/redirect scenarios
This commit is contained in:
parent
9814f3bfbc
commit
03b63e2c2a
|
|
@ -1,6 +1,6 @@
|
||||||
<rewrite>
|
<rewrite>
|
||||||
<rules>
|
<rules>
|
||||||
<rule name="" stopProcessing="true">
|
<rule name="Rewrite 20 to 10" stopProcessing="true">
|
||||||
<match url="(.*)" />
|
<match url="(.*)" />
|
||||||
<conditions>
|
<conditions>
|
||||||
<add input="{QUERY_STRING}" pattern="id=20" />
|
<add input="{QUERY_STRING}" pattern="id=20" />
|
||||||
|
|
|
||||||
|
|
@ -8,28 +8,34 @@ namespace Microsoft.AspNetCore.Rewrite.Logging
|
||||||
{
|
{
|
||||||
internal static class RewriteMiddlewareLoggingExtensions
|
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, 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> _urlRewriteDidNotMatchRule;
|
||||||
private static readonly Action<ILogger, string, Exception> _urlRewriteMatchedRule;
|
private static readonly Action<ILogger, string, Exception> _urlRewriteMatchedRule;
|
||||||
private static readonly Action<ILogger, Exception> _modRewriteDidNotMatchRule;
|
private static readonly Action<ILogger, Exception> _modRewriteDidNotMatchRule;
|
||||||
private static readonly Action<ILogger, Exception> _modRewriteMatchedRule;
|
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()
|
static RewriteMiddlewareLoggingExtensions()
|
||||||
{
|
{
|
||||||
_requestContinueResults = LoggerMessage.Define(
|
_requestContinueResults = LoggerMessage.Define<string>(
|
||||||
LogLevel.Debug,
|
LogLevel.Debug,
|
||||||
1,
|
1,
|
||||||
"Request is continuing in applying rules.");
|
"Request is continuing in applying rules. Current url is {currentUrl}");
|
||||||
|
|
||||||
_requestResponseComplete = LoggerMessage.Define<string, int>(
|
_requestResponseComplete = LoggerMessage.Define<string, int>(
|
||||||
LogLevel.Debug,
|
LogLevel.Debug,
|
||||||
2,
|
2,
|
||||||
"Request is done processing, Location header '{Location}' with status code '{StatusCode}'.");
|
"Request is done processing. Location header '{Location}' with status code '{StatusCode}'.");
|
||||||
_requestStopRules = LoggerMessage.Define(
|
|
||||||
|
_requestStopRules = LoggerMessage.Define<string>(
|
||||||
LogLevel.Debug,
|
LogLevel.Debug,
|
||||||
3,
|
3,
|
||||||
"Request is done applying rules.");
|
"Request is done applying rules. Url was rewritten to {rewrittenUrl}");
|
||||||
|
|
||||||
_urlRewriteDidNotMatchRule = LoggerMessage.Define<string>(
|
_urlRewriteDidNotMatchRule = LoggerMessage.Define<string>(
|
||||||
LogLevel.Debug,
|
LogLevel.Debug,
|
||||||
4,
|
4,
|
||||||
|
|
@ -49,11 +55,26 @@ namespace Microsoft.AspNetCore.Rewrite.Logging
|
||||||
LogLevel.Debug,
|
LogLevel.Debug,
|
||||||
7,
|
7,
|
||||||
"Request matched current ModRewriteRule.");
|
"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)
|
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);
|
_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)
|
public static void UrlRewriteDidNotMatchRule(this ILogger logger, string name)
|
||||||
|
|
@ -80,9 +101,25 @@ namespace Microsoft.AspNetCore.Rewrite.Logging
|
||||||
{
|
{
|
||||||
_modRewriteDidNotMatchRule(logger, null);
|
_modRewriteDidNotMatchRule(logger, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void ModRewriteMatchedRule(this ILogger logger)
|
public static void ModRewriteMatchedRule(this ILogger logger)
|
||||||
{
|
{
|
||||||
_modRewriteMatchedRule(logger, null);
|
_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 System.Text.RegularExpressions;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Net.Http.Headers;
|
using Microsoft.Net.Http.Headers;
|
||||||
|
using Microsoft.AspNetCore.Rewrite.Logging;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Rewrite.Internal
|
namespace Microsoft.AspNetCore.Rewrite.Internal
|
||||||
{
|
{
|
||||||
|
|
@ -46,6 +47,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal
|
||||||
initMatchResults = InitialMatch.Match(path.ToString().Substring(1));
|
initMatchResults = InitialMatch.Match(path.ToString().Substring(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (initMatchResults.Success)
|
if (initMatchResults.Success)
|
||||||
{
|
{
|
||||||
var newPath = initMatchResults.Result(Replacement);
|
var newPath = initMatchResults.Result(Replacement);
|
||||||
|
|
@ -78,6 +80,8 @@ namespace Microsoft.AspNetCore.Rewrite.Internal
|
||||||
{
|
{
|
||||||
response.Headers[HeaderNames.Location] = pathBase + newPath;
|
response.Headers[HeaderNames.Location] = pathBase + newPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
context.Logger?.RedirectedSummary(newPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Rewrite.Logging;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Rewrite.Internal
|
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);
|
var newUrl = new StringBuilder().Append("https://").Append(host).Append(req.PathBase).Append(req.Path).Append(req.QueryString);
|
||||||
context.HttpContext.Response.Redirect(newUrl.ToString());
|
context.HttpContext.Response.Redirect(newUrl.ToString());
|
||||||
context.Result = RuleResult.EndResponse;
|
context.Result = RuleResult.EndResponse;
|
||||||
|
context.Logger?.RedirectedToHttps();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ using System;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Http.Extensions;
|
using Microsoft.AspNetCore.Http.Extensions;
|
||||||
|
using Microsoft.AspNetCore.Rewrite.Logging;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Rewrite.Internal
|
namespace Microsoft.AspNetCore.Rewrite.Internal
|
||||||
{
|
{
|
||||||
|
|
@ -103,6 +104,8 @@ namespace Microsoft.AspNetCore.Rewrite.Internal
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
context.Logger?.RewriteSummary(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -75,10 +75,11 @@ namespace Microsoft.AspNetCore.Rewrite
|
||||||
foreach (var rule in _options.Rules)
|
foreach (var rule in _options.Rules)
|
||||||
{
|
{
|
||||||
rule.ApplyRule(rewriteContext);
|
rule.ApplyRule(rewriteContext);
|
||||||
|
var currentUrl = new Lazy<string>(() => context.Request.Path + context.Request.QueryString);
|
||||||
switch (rewriteContext.Result)
|
switch (rewriteContext.Result)
|
||||||
{
|
{
|
||||||
case RuleResult.ContinueRules:
|
case RuleResult.ContinueRules:
|
||||||
_logger.RewriteMiddlewareRequestContinueResults();
|
_logger.RewriteMiddlewareRequestContinueResults(currentUrl.Value);
|
||||||
break;
|
break;
|
||||||
case RuleResult.EndResponse:
|
case RuleResult.EndResponse:
|
||||||
_logger.RewriteMiddlewareRequestResponseComplete(
|
_logger.RewriteMiddlewareRequestResponseComplete(
|
||||||
|
|
@ -86,7 +87,7 @@ namespace Microsoft.AspNetCore.Rewrite
|
||||||
context.Response.StatusCode);
|
context.Response.StatusCode);
|
||||||
return TaskCache.CompletedTask;
|
return TaskCache.CompletedTask;
|
||||||
case RuleResult.SkipRemainingRules:
|
case RuleResult.SkipRemainingRules:
|
||||||
_logger.RewriteMiddlewareRequestStopRules();
|
_logger.RewriteMiddlewareRequestStopRules(currentUrl.Value);
|
||||||
return _next(context);
|
return _next(context);
|
||||||
default:
|
default:
|
||||||
throw new ArgumentOutOfRangeException($"Invalid rule termination {rewriteContext.Result}");
|
throw new ArgumentOutOfRangeException($"Invalid rule termination {rewriteContext.Result}");
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue