1 // Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. 2 3 using System.Collections.Generic; 4 using System.Diagnostics.Contracts; 5 using System.Globalization; 6 using System.Linq; 7 using System.Net.Http.Formatting; 8 using System.Text; 9 using System.Web.Http.Controllers; 10 using System.Web.Http.ModelBinding; 11 using System.Web.Http.ModelBinding.Binders; 12 using System.Web.Http.Properties; 13 using System.Web.Http.Routing; 14 using System.Web.Http.ValueProviders; 15 using System.Web.Http.ValueProviders.Providers; 16 17 namespace System.Web.Http.Tracing 18 { 19 /// <summary> 20 /// General purpose utilities to format strings used in tracing. 21 /// </summary> 22 internal static class FormattingUtilities 23 { 24 public static readonly string NullMessage = "null"; 25 ActionArgumentsToString(IDictionary<string, object> actionArguments)26 public static string ActionArgumentsToString(IDictionary<string, object> actionArguments) 27 { 28 Contract.Assert(actionArguments != null); 29 return string.Join(", ", 30 actionArguments.Keys.Select<string, string>( 31 (k) => k + "=" + ValueToString(actionArguments[k], CultureInfo.CurrentCulture))); 32 } 33 ActionDescriptorToString(HttpActionDescriptor actionDescriptor)34 public static string ActionDescriptorToString(HttpActionDescriptor actionDescriptor) 35 { 36 Contract.Assert(actionDescriptor != null); 37 38 string parameterList = string.Join(", ", 39 actionDescriptor.GetParameters().Select<HttpParameterDescriptor, string>( 40 (p) => p.ParameterType.Name + " " + p.ParameterName)); 41 42 return actionDescriptor.ActionName + "(" + parameterList + ")"; 43 } 44 ActionInvokeToString(HttpActionContext actionContext)45 public static string ActionInvokeToString(HttpActionContext actionContext) 46 { 47 Contract.Assert(actionContext != null); 48 return ActionInvokeToString(actionContext.ActionDescriptor.ActionName, actionContext.ActionArguments); 49 } 50 ActionInvokeToString(string actionName, IDictionary<string, object> arguments)51 public static string ActionInvokeToString(string actionName, IDictionary<string, object> arguments) 52 { 53 Contract.Assert(actionName != null); 54 Contract.Assert(arguments != null); 55 56 return actionName + "(" + ActionArgumentsToString(arguments) + ")"; 57 } 58 FormattersToString(IEnumerable<MediaTypeFormatter> formatters)59 public static string FormattersToString(IEnumerable<MediaTypeFormatter> formatters) 60 { 61 Contract.Assert(formatters != null); 62 63 return String.Join(", ", formatters.Select<MediaTypeFormatter, string>((f) => f.GetType().Name)); 64 } 65 ModelBinderToString(ModelBinderProvider provider)66 public static string ModelBinderToString(ModelBinderProvider provider) 67 { 68 Contract.Assert(provider != null); 69 70 CompositeModelBinderProvider composite = provider as CompositeModelBinderProvider; 71 if (composite == null) 72 { 73 return provider.GetType().Name; 74 } 75 76 string modelBinderList = string.Join(", ", composite.Providers.Select<ModelBinderProvider, string>(ModelBinderToString)); 77 78 return provider.GetType().Name + "(" + modelBinderList + ")"; 79 } 80 ModelStateToString(ModelStateDictionary modelState)81 public static string ModelStateToString(ModelStateDictionary modelState) 82 { 83 Contract.Assert(modelState != null); 84 85 if (modelState.IsValid) 86 { 87 return String.Empty; 88 } 89 90 StringBuilder modelStateBuilder = new StringBuilder(); 91 foreach (string key in modelState.Keys) 92 { 93 ModelState state = modelState[key]; 94 if (state.Errors.Count > 0) 95 { 96 foreach (ModelError error in state.Errors) 97 { 98 string errorString = Error.Format(SRResources.TraceModelStateErrorMessage, 99 key, 100 error.ErrorMessage); 101 if (modelStateBuilder.Length > 0) 102 { 103 modelStateBuilder.Append(','); 104 } 105 106 modelStateBuilder.Append(errorString); 107 } 108 } 109 } 110 111 return modelStateBuilder.ToString(); 112 } 113 RouteToString(IHttpRouteData routeData)114 public static string RouteToString(IHttpRouteData routeData) 115 { 116 Contract.Assert(routeData != null); 117 118 return String.Join(",", routeData.Values.Select((pair) => Error.Format("{0}:{1}", pair.Key, pair.Value))); 119 } 120 ValueProviderToString(IValueProvider provider)121 public static string ValueProviderToString(IValueProvider provider) 122 { 123 Contract.Assert(provider != null); 124 125 CompositeValueProvider composite = provider as CompositeValueProvider; 126 if (composite == null) 127 { 128 return provider.GetType().Name; 129 } 130 131 string providerList = string.Join(", ", composite.Select<IValueProvider, string>(ValueProviderToString)); 132 return provider.GetType().Name + "(" + providerList + ")"; 133 } 134 ValueToString(object value, CultureInfo cultureInfo)135 public static string ValueToString(object value, CultureInfo cultureInfo) 136 { 137 Contract.Assert(cultureInfo != null); 138 139 if (value == null) 140 { 141 return NullMessage; 142 } 143 144 return Convert.ToString(value, cultureInfo); 145 } 146 } 147 } 148