4
4
using System . Linq ;
5
5
using System . Net ;
6
6
using System . Text ;
7
- using System . Text . RegularExpressions ;
8
7
9
8
namespace TodoWebApp . Logging
10
9
{
@@ -17,8 +16,8 @@ public class LoggingService : IHttpContextLoggingHandler, IHttpObjectConverter
17
16
private const int RESPONSE_SIZE = 1000 ;
18
17
19
18
private static readonly string [ ] textBasedHeaderNames = { "Accept" , "Content-Type" } ;
20
- private static readonly string [ ] textBasedFragments = { "application/json" , "application/xml" , "text/" } ;
21
- private static readonly Regex textBasedRegex = new Regex ( "/api/" , RegexOptions . Compiled | RegexOptions . CultureInvariant | RegexOptions . IgnoreCase ) ;
19
+ private static readonly string [ ] textBasedHeaderValues = { "application/json" , "application/xml" , "text/" } ;
20
+ private const string ACCEPTABLE_REQUEST_URL_PREFIX = "/api/" ;
22
21
private readonly ILogger logger ;
23
22
24
23
/// <summary>
@@ -43,10 +42,11 @@ public bool ShouldLog(HttpContext httpContext)
43
42
}
44
43
45
44
var result = IsTextBased ( httpContext . Request ) ;
45
+ var willBeLoggedOutcome = result ? string . Empty : " NOT" ;
46
46
47
47
if ( logger . IsEnabled ( LogLevel . Debug ) )
48
48
{
49
- logger . LogDebug ( $ "HTTP context { httpContext . TraceIdentifier } will be logged: { result . ToString ( ) . ToLowerInvariant ( ) } ") ;
49
+ logger . LogDebug ( $ "HTTP context { httpContext . TraceIdentifier } will{ willBeLoggedOutcome } be logged") ;
50
50
}
51
51
52
52
return result ;
@@ -119,13 +119,13 @@ public string ToLogMessage(HttpResponse httpResponse)
119
119
private static bool IsTextBased ( HttpRequest httpRequest )
120
120
{
121
121
return textBasedHeaderNames . Any ( headerName => IsTextBased ( httpRequest , headerName ) )
122
- || textBasedRegex . IsMatch ( httpRequest . Path ) ;
122
+ || httpRequest . Path . ToUriComponent ( ) . StartsWith ( ACCEPTABLE_REQUEST_URL_PREFIX ) ;
123
123
}
124
124
125
125
private static bool IsTextBased ( HttpRequest httpRequest , string headerName )
126
126
{
127
- return httpRequest . Headers . TryGetValue ( headerName , out var headerValue )
128
- && textBasedFragments . Any ( fragment => headerValue . Contains ( fragment ) ) ;
127
+ return httpRequest . Headers . TryGetValue ( headerName , out var headerValues )
128
+ && textBasedHeaderValues . Any ( textBasedHeaderValue => headerValues . Any ( headerValue => headerValue . StartsWith ( textBasedHeaderValue ) ) ) ;
129
129
}
130
130
}
131
131
}
0 commit comments