Skip to content

Instantly share code, notes, and snippets.

@RomaRudyak
Last active May 23, 2019 08:35
Show Gist options
  • Save RomaRudyak/1308ab80fb3d4f893cb17b98897b0317 to your computer and use it in GitHub Desktop.
Save RomaRudyak/1308ab80fb3d4f893cb17b98897b0317 to your computer and use it in GitHub Desktop.
Logging DelegatingHandler for HttpMessageHandler request in .NET
public class LoggerHttpMessageHandler : DelegatingHandler
{
public LoggerHttpMessageHandler(HttpMessageHandler innerHandler)
: base(innerHandler)
{
}
protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var st = new Stopwatch();
try
{
st.Start();
var res = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
st.Stop();
await LogResponse(res, st.Elapsed).ConfigureAwait(false);
return res;
}
catch (Exception ex)
{
st.Stop();
await LogEx(request, ex, st.Elapsed)
.ConfigureAwait(false);
throw ex;
}
}
private async Task LogEx(HttpRequestMessage request, Exception ex, TimeSpan elapsed)
{
var sb = new StringBuilder();
sb.AppendLine("\n--------------------");
await AppendRequestLog(sb, request)
.ConfigureAwait(false);
sb.AppendLine("--------------------");
AppendEllapsedTimeLog(sb, elapsed);
sb.AppendLine("--------------------");
AppendExceptionLog(ex, sb);
sb.AppendLine("--------------------\n");
Debug.WriteLine(sb.ToString());
}
private async Task LogResponse(HttpResponseMessage response, TimeSpan elapsed)
{
var sb = new StringBuilder();
sb.AppendLine("\n--------------------");
await AppendRequestLog(sb, response.RequestMessage)
.ConfigureAwait(false);
sb.AppendLine("--------------------");
AppendEllapsedTimeLog(sb, elapsed);
sb.AppendLine("--------------------");
await AppendResponseLog(sb, response)
.ConfigureAwait(false);
sb.AppendLine("--------------------\n");
Debug.WriteLine(sb.ToString());
}
private static async Task AppendResponseLog(StringBuilder sb, HttpResponseMessage response)
{
sb.AppendLine(response.ToString());
if (response.Content != null)
{
try
{
var canLogContent = response.Content.Headers.TryGetValues("Content-Type", out IEnumerable<string> values)
&& values.Any(c => c.StartsWith("application/json", StringComparison.Ordinal));
var body = "";
if (canLogContent)
{
body = await response.Content.ReadAsStringAsync()
.ConfigureAwait(false);
}
sb.AppendLine($"BODY:\n{body}");
}
#pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body
catch (Exception)
{
}
#pragma warning restore RECS0022 // A catch clause that catches System.Exception and has an empty body
}
}
private static void AppendExceptionLog(Exception ex, StringBuilder sb)
{
sb.Append("Exception:\n");
sb.AppendLine(ex.ToString());
}
private static void AppendEllapsedTimeLog(StringBuilder sb, TimeSpan elapsed)
{
sb.AppendLine($"Respond in: {elapsed.Milliseconds}ms");
}
private static async Task AppendRequestLog(StringBuilder sb, HttpRequestMessage request)
{
sb.AppendLine(request.ToString());
if (request.Content != null)
{
var body = await request.Content.ReadAsStringAsync().ConfigureAwait(false);
sb.AppendLine($"BODY:\n{body}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment