• 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/MTIxMA==

Related Code
Extension to the HttpClient
PagedList class and paging code for ASP.Net MVC (Nothing new but kind of usefull.)
Unity Service Locator for ASP.NET MVC 3.0 Beta 1
Fork of GZIP your Actions in MVC if not used as standard in IIS7 or only use IIS6
WCF Service factory for using Microsoft Unity 2.0 to create an instance of a Service
Google Analytics helper for ASP.NET Razor
Unity Controller Factory for ASP.NET MVC 3.0
ASP.NET MVC 4.0 Web API Message logger for .Net 4.5
UnitOfWork Action filter for ASP.NET MVC and nHibernate
Make sure the web.config pages/namespace can be used together with ASP.NET MVC 3 P1 and Razor
StringFormat Extension method for HtmlHelper ASP.NET MVC
Get Cookies in Web Api Asp .net Mvc 4 RC
GZIP your Actions in MVC if not used as standard in IIS7 or only use IIS6

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

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

0
961 0 0 0 0 0

public abstract class MessageHandler : DelegatingHandler
{
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        var corrId =string.Format("{0}{1}", DateTime.Now.Ticks, Thread.CurrentThread.ManagedThreadId);

        return request.Content.ReadAsByteArrayAsync()
            .ContinueWith(t => IncommingMessageAsync(corrId, t.Result)
            .ContinueWith(t2 => base.SendAsync(request, cancellationToken)
            .ContinueWith(task =>
            {
                 task.Result.Content.ReadAsByteArrayAsync()
                     .ContinueWith(t3 => OutgoingMessageAsync(corrId, t3.Result),cancellationToken);

                return task.Result;

            },cancellationToken),
            cancellationToken)
            .Unwrap())
            .Unwrap();
    }


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


public class MessageLoggingHandler : MessageHandler
{
    protected override Task IncommingMessageAsync(string correlationId, byte[] message)
    {
        var tcs = new TaskCompletionSource<object>();

        Debug.WriteLine(string.Format("{0} - Incomming messages: {1}", correlationId, Encoding.UTF8.GetString(message)));

        tcs.SetResult(null);

        return tcs.Task;
    }


    protected override Task OutgoingMessageAsync(string correlationId, byte[] message)
    {
        var tcs = new TaskCompletionSource<object>();

        Debug.WriteLine(string.Format("{0} - Outgoing messages: {1}", correlationId, Encoding.UTF8.GetString(message)));

        tcs.SetResult(null);

        return tcs.Task;
    }
}

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

Mark '.net4' tag as 'like'

Mark '.net4' tag as 'ignore'

aspmvc

Mark 'aspmvc' tag as 'like'

Mark 'aspmvc' 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:30 PM

Fork

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


0 Feedback


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