From 1b76152b5c7ba0a7f0e544bb3593c3884986b22d Mon Sep 17 00:00:00 2001 From: Cesar Blum Silveira Date: Wed, 30 Dec 2015 13:46:08 -0800 Subject: [PATCH] Enable logging in RequestUriBuilder (#169). --- src/Microsoft.Net.Http.Server/LogHelper.cs | 4 ++-- .../RequestProcessing/Request.cs | 2 +- .../RequestProcessing/RequestUriBuilder.cs | 20 +++++++++---------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Microsoft.Net.Http.Server/LogHelper.cs b/src/Microsoft.Net.Http.Server/LogHelper.cs index da3c22ded3..98b009afa2 100644 --- a/src/Microsoft.Net.Http.Server/LogHelper.cs +++ b/src/Microsoft.Net.Http.Server/LogHelper.cs @@ -51,7 +51,7 @@ namespace Microsoft.Net.Http.Server } } - internal static void LogDebug(ILogger logger, string data) + internal static void LogDebug(ILogger logger, string location, string data) { if (logger == null) { @@ -59,7 +59,7 @@ namespace Microsoft.Net.Http.Server } else { - logger.LogDebug(data); + logger.LogDebug(location + "; " + data); } } diff --git a/src/Microsoft.Net.Http.Server/RequestProcessing/Request.cs b/src/Microsoft.Net.Http.Server/RequestProcessing/Request.cs index e3d49d2eea..67fd440e90 100644 --- a/src/Microsoft.Net.Http.Server/RequestProcessing/Request.cs +++ b/src/Microsoft.Net.Http.Server/RequestProcessing/Request.cs @@ -391,7 +391,7 @@ namespace Microsoft.Net.Http.Server { get { - return RequestUriBuilder.GetRequestPath(_rawUrl, _cookedUrlPath); + return RequestUriBuilder.GetRequestPath(_rawUrl, _cookedUrlPath, RequestContext.Logger); } } diff --git a/src/Microsoft.Net.Http.Server/RequestProcessing/RequestUriBuilder.cs b/src/Microsoft.Net.Http.Server/RequestProcessing/RequestUriBuilder.cs index 8461ae531a..26169bfa36 100644 --- a/src/Microsoft.Net.Http.Server/RequestProcessing/RequestUriBuilder.cs +++ b/src/Microsoft.Net.Http.Server/RequestProcessing/RequestUriBuilder.cs @@ -26,6 +26,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.Text; +using Microsoft.Extensions.Logging; namespace Microsoft.Net.Http.Server { @@ -52,18 +53,22 @@ namespace Microsoft.Net.Http.Server private List _rawOctets; private string _rawPath; + private ILogger _logger; + static RequestUriBuilder() { Utf8Encoding = new UTF8Encoding(false, true); } - private RequestUriBuilder(string rawUri, string cookedUriPath) + private RequestUriBuilder(string rawUri, string cookedUriPath, ILogger logger) { Debug.Assert(!string.IsNullOrEmpty(rawUri), "Empty raw URL."); Debug.Assert(!string.IsNullOrEmpty(cookedUriPath), "Empty cooked URL path."); + Debug.Assert(logger != null, "Null logger."); this._rawUri = rawUri; this._cookedUriPath = AddSlashToAsteriskOnlyPath(cookedUriPath); + this._logger = logger; } private enum ParsingResult @@ -74,9 +79,9 @@ namespace Microsoft.Net.Http.Server } // Process only the path. - internal static string GetRequestPath(string rawUri, string cookedUriPath) + internal static string GetRequestPath(string rawUri, string cookedUriPath, ILogger logger) { - RequestUriBuilder builder = new RequestUriBuilder(rawUri, cookedUriPath); + RequestUriBuilder builder = new RequestUriBuilder(rawUri, cookedUriPath, logger); return builder.GetPath(); } @@ -167,7 +172,7 @@ namespace Microsoft.Net.Http.Server byte encodedValue; if (!byte.TryParse(escapedCharacter, NumberStyles.HexNumber, null, out encodedValue)) { - LogWarning(nameof(AddPercentEncodedOctetToRawOctetsList), "Can't convert code point: " + escapedCharacter); + LogHelper.LogDebug(_logger, nameof(AddPercentEncodedOctetToRawOctetsList), "Can't convert code point: " + escapedCharacter); return false; } @@ -198,7 +203,7 @@ namespace Microsoft.Net.Http.Server } catch (DecoderFallbackException e) { - LogWarning(nameof(EmptyDecodeAndAppendDecodedOctetsList), "Can't convert bytes: " + GetOctetsAsString(_rawOctets), e.Message); + LogHelper.LogDebug(_logger, nameof(EmptyDecodeAndAppendDecodedOctetsList), "Can't convert bytes: " + GetOctetsAsString(_rawOctets) + ": " + e.Message); } return false; @@ -307,10 +312,5 @@ namespace Microsoft.Net.Http.Server return path; } - - private void LogWarning(string methodName, string message, params object[] args) - { - // TODO: Verbose log - } } }