Created
February 22, 2012 04:39
-
-
Save darrelmiller/1881457 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public class LoggingFormatterSelector : IFormatterSelector | |
{ | |
private readonly FormatterSelector _FormatterSelector = new FormatterSelector(); | |
public new MediaTypeFormatter SelectReadFormatter(Type type, FormatterContext formatterContext, IEnumerable<MediaTypeFormatter> formatters) | |
{ | |
string descriptFormatterContext = GetDescriptFormatterContext(formatterContext); | |
Console.WriteLine("Selecting Read Formatter for type {0} based on formatter context: {1}", type.Name, descriptFormatterContext); | |
var mediaTypeFormatter = _FormatterSelector.SelectReadFormatter(type, formatterContext, formatters); | |
Console.WriteLine("Selected Read Formatter : {0}", mediaTypeFormatter.GetType().Name); | |
return mediaTypeFormatter; | |
} | |
public new MediaTypeFormatter SelectWriteFormatter(Type type, FormatterContext formatterContext, IEnumerable<MediaTypeFormatter> formatters, out MediaTypeHeaderValue mediaType) | |
{ | |
string descriptFormatterContext = GetDescriptFormatterContext(formatterContext); | |
Console.WriteLine("Selecting Write Formatter for type {0} based on formatter context: {1}", type.Name, descriptFormatterContext); | |
var mediaTypeFormatter = _FormatterSelector.SelectWriteFormatter(type, formatterContext, formatters, out mediaType); | |
Console.WriteLine("Selected Write Formatter : {0} {1}", mediaTypeFormatter.GetType().Name, mediaType); | |
return mediaTypeFormatter; | |
} | |
private string GetDescriptFormatterContext(FormatterContext formatterContext) | |
{ | |
return String.Format("ContentType {0}, HasRequest {1}, HasResponse {2}, IsRead {3} ", | |
formatterContext.ContentType , | |
formatterContext.Request != null, | |
formatterContext.Response != null, | |
formatterContext.IsRead); | |
} | |
} | |
} | |
Which can be plugged in like this, | |
config.ServiceResolver.SetService(typeof(IFormatterSelector),new LoggingFormatterSelector()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment