• Code
  • Tags
  • Users
  • Titles
  • Log in
  • Feedback
  • FAQ
Share Code
Welcome to ForkCan.com

ForkCan is all about sharing code in a social way.

Discuss, debate or argue with other devs about their or your own code.

Give other devs feedback or make a Fork (Make a better version of a shared code).

Rate the code, if you use the code mark it as used so others can see if the shared code is used by someone.

Help each other to be better devs and to be more productive.


Features not working yet:

Flag a post


QR Code

Tiny Url

http://4kcan.com/s/MTIwOQ==

Related Code
Get Cookies in Web Api Asp .net Mvc 4 RC
ASP.NET MVC 4.0 Web API Message logger for .Net 4.0
Extension to the HttpClient

ASP.NET MVC 4.0 Web API Message logger for .Net 4.5

The following code can be used to log inncomming and outgoing messages

1
866 0 1 0 1 0

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 result = await request.Content.ReadAsByteArrayAsync();

        await IncommingMessageAsync(corrId, result);

        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, responseMessage);

        return response;
    }


    protected abstract Task IncommingMessageAsync(string correlationId, byte[] message);
    protected abstract Task OutgoingMessageAsync(string correlationId, byte[] message);
}



public class MessageLoggingHandler : MessageHandler
{
    protected override async Task IncommingMessageAsync(string correlationId, byte[] message)
    {
        await Task.Run(() =>
            Debug.WriteLine(string.Format("{0} - Incomming messages: {1}", correlationId, Encoding.UTF8.GetString(message))));
    }


    protected override async Task OutgoingMessageAsync(string correlationId, byte[] message)
    {
        await Task.Run(() =>
            Debug.WriteLine(string.Format("{0} - Outgoing messages: {1}", correlationId, Encoding.UTF8.GetString(message))));
    }
}

The MessageHandler need to be registered to be used, it can be done in the global.asax:

GlobalConfiguration.Configuration.MessageHandlers.Add(new MessageLoggingHandler());

Note: Because the messages is read into a buffer, there will be two compy of the data, so be carefull when it comes to large messages.


Share: twitter | facebook   Action: used | fork | flag

.net4.5

Mark '.net4.5' tag as 'like'

Mark '.net4.5' tag as 'ignore'

c#

Mark 'c#' tag as 'like'

Mark 'c#' tag as 'ignore'

webapi

Mark 'webapi' tag as 'like'

Mark 'webapi' tag as 'ignore'


 @fredrikn "I'm the master"
3.11k
February 16, 2012 5:25 PM
edited June 12, 2012 7:58 AM

Fork

 ASP.NET MVC 4.0 Web API Message logger for .Net 4.5 -  @fredrikn Thursday 16, 2012 5:25 PM


2 Feedback

Sweet! Thanks! -  Timothy Lee Russell Thursday 30, 2012 6:20 AM
"Incoming" only has one M. -  Timothy Lee Russell Thursday 30, 2012 6:20 AM

You must log in before you can give any feedback


0 Discussion(s)

Newest Oldest

You must log in before you can post a comment

Squeed
Made by: Fredrik Normén 2010