Code to log message received or send from Web API:
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Program.Api.Web.ActionFilters
{
/// <br />
<br />
<summary>
/// Class to log the request/response received from the client
/// </summary>
public abstract class MessageHandler : DelegatingHandler
{
protected override async Task<httpresponsemessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var corrId = string.Format("{0}{1}", DateTime.Now.Ticks, Thread.CurrentThread.ManagedThreadId);
var requestInfo = string.Format("{0} {1}", request.Method, request.RequestUri);
var requestMessage = await request.Content.ReadAsByteArrayAsync();
await IncommingMessageAsync(corrId, requestInfo, requestMessage);
var response = await base.SendAsync(request, cancellationToken);
byte[] responseMessage;
if (response.IsSuccessStatusCode)
responseMessage = await response.Content.ReadAsByteArrayAsync();
else
responseMessage = Encoding.UTF8.GetBytes(response.ReasonPhrase);
await OutgoingMessageAsync(corrId, requestInfo, responseMessage);
return response;
}
protected abstract Task IncommingMessageAsync(string correlationId, string requestInfo, byte[] message);
protected abstract Task OutgoingMessageAsync(string correlationId, string requestInfo, byte[] message);
}
/// <summary>
/// The logger class which will log to the database or text file
/// </summary>
public class MessageLoggingHandler : MessageHandler
{
protected override async Task IncommingMessageAsync(string correlationId, string requestInfo, byte[] message)
{
if(WebConfig.LogMessages) // Based on web.config property
await Task.Run(() =>
Trace.WriteLine(string.Format("{0} - Request: {1}\r\n{2}", correlationId, requestInfo, Encoding.UTF8.GetString(message))));
}
protected override async Task OutgoingMessageAsync(string correlationId, string requestInfo, byte[] message)
{
if (WebConfig.LogMessages)
await Task.Run(() =>
Trace.WriteLine(string.Format("{0} - Response: {1}\r\n{2}", correlationId, requestInfo, Encoding.UTF8.GetString(message))));
}
}
}
0 comments:
Post a Comment