-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor loggerhandler * support multipe loglevel for exceptions * better logger format in CI * add test for logging * add test for default logging output
- Loading branch information
Showing
9 changed files
with
274 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
commercetools.Sdk/commercetools.Base.Client/DefaultHttpLogger.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Text.RegularExpressions; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace commercetools.Base.Client | ||
{ | ||
public class DefaultHttpLogger : IHttpLogger | ||
{ | ||
public async Task LogRequestBody(ILogger logger, LogLevel logLevel, HttpRequestMessage request) | ||
{ | ||
if (logger.IsEnabled(logLevel)) | ||
{ | ||
var body = await (request.Content?.ReadAsStringAsync() ?? Task.FromResult("")); | ||
logger.Log(logLevel, "{HttpMethod} {Uri} {Headers} {Body}", request.Method.Method, | ||
request.RequestUri.AbsoluteUri, RedactAuthorizationHeader(request.Headers), SecuredBody(body)); | ||
} | ||
} | ||
|
||
public async Task LogResponseBody(ILogger logger, LogLevel logLevel, HttpRequestMessage request, HttpResponseMessage response, long elapsed) | ||
{ | ||
if (logger.IsEnabled(logLevel)) | ||
{ | ||
var body = await (response.Content?.ReadAsStringAsync() ?? Task.FromResult("")); | ||
logger.Log(logLevel, "{HttpMethod} {Uri} {StatusCode} {Timing} {Headers} {Body}", request.Method.Method, | ||
request.RequestUri.AbsoluteUri, (int)response.StatusCode, elapsed, RedactAuthorizationHeader(request.Headers), SecuredBody(body)); | ||
} | ||
} | ||
|
||
public void Log(ILogger logger, LogLevel logLevel, HttpRequestMessage request) | ||
{ | ||
if (logger.IsEnabled(logLevel)) | ||
{ | ||
logger.Log(logLevel, "{HttpMethod} {Uri} {Headers}", request.Method.Method, | ||
request.RequestUri.AbsoluteUri, RedactAuthorizationHeader(request.Headers)); | ||
} | ||
} | ||
|
||
public void Log(ILogger logger, LogLevel level, HttpRequestMessage request, HttpResponseMessage response, long elapsed) | ||
{ | ||
if (logger.IsEnabled(level)) | ||
{ | ||
logger.Log(level, "{HttpMethod} {Uri} {StatusCode} {Timing} {CorrelationId} {ServerTiming}", request.Method.Method, | ||
request.RequestUri.AbsoluteUri, (int)response.StatusCode, elapsed, GetCorrelationId(response.Headers), GetServerTiming(response.Headers)); | ||
} | ||
} | ||
|
||
public void Log(ILogger logger, LogLevel logLevel, HttpRequestMessage request, ApiHttpException exception, long elapsed) | ||
{ | ||
if (logger.IsEnabled(logLevel)) | ||
{ | ||
logger.Log(logLevel, "{HttpMethod} {Uri} {StatusCode} {Timing} {CorrelationId} {ServerTiming}", request.Method.Method, | ||
request.RequestUri.AbsoluteUri, exception.StatusCode, elapsed, GetCorrelationId(exception.Headers), GetServerTiming(exception.Headers)); | ||
} | ||
} | ||
|
||
|
||
private static string RedactAuthorizationHeader(HttpRequestHeaders headers) | ||
{ | ||
var headString = from header in headers | ||
where header.Key.ToLower() != "authorization" | ||
select header.Key + ": " + string.Join(", ", header.Value); | ||
|
||
return "[" + string.Join(", ", headString) + "]"; | ||
} | ||
|
||
private static string SecuredBody(string body) | ||
{ | ||
if (body != null) | ||
return Regex.Replace(body, "(\"\\w*([Pp]ass|access_token|refresh_token)\\w*\"):\\W*\"[^\"]*\"", | ||
"$1:\"**removed from output**\""); | ||
return null; | ||
} | ||
|
||
private static string GetCorrelationId(ApiHttpHeaders headers) | ||
{ | ||
return headers.GetFirst("X-Correlation-Id") ?? "-"; | ||
} | ||
|
||
private static string GetCorrelationId(HttpResponseHeaders headers) | ||
{ | ||
return GetHeader(headers, "X-Correlation-ID"); | ||
} | ||
|
||
private static string GetHeader(HttpResponseHeaders headers, string headerName) | ||
{ | ||
var headerValue = "-"; | ||
|
||
if (headers.TryGetValues(headerName, out var values)) | ||
{ | ||
headerValue = values.First(); | ||
} | ||
|
||
return headerValue; | ||
} | ||
|
||
private static string GetServerTiming(HttpResponseHeaders headers) | ||
{ | ||
return GetHeader(headers, "Server-Timing"); | ||
} | ||
|
||
private static string GetServerTiming(ApiHttpHeaders headers) | ||
{ | ||
return headers.GetFirst("Server-Timing") ?? "-"; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
commercetools.Sdk/commercetools.Base.Client/IHttpLogger.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace commercetools.Base.Client | ||
{ | ||
public interface IHttpLogger | ||
{ | ||
void Log(ILogger logger, LogLevel level, HttpRequestMessage request); | ||
void Log(ILogger logger, LogLevel level, HttpRequestMessage request, HttpResponseMessage response, long elapsed); | ||
void Log(ILogger logger, LogLevel logLevel, HttpRequestMessage request, ApiHttpException exception, long elapsed); | ||
|
||
Task LogRequestBody(ILogger logger, LogLevel logLevel, HttpRequestMessage request); | ||
|
||
Task LogResponseBody(ILogger logger, LogLevel logLevel, HttpRequestMessage request, | ||
HttpResponseMessage response, long elapsed); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
commercetools.Sdk/commercetools.Base.Client/ILoggerHandlerOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace commercetools.Base.Client | ||
{ | ||
public interface ILoggerHandlerOptions | ||
{ | ||
LogLevel ResponseLogEvent { get; } | ||
LogLevel DefaultExceptionLogEvent { get; } | ||
Dictionary<Type, LogLevel> ExceptionLogEvents { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.