-
-
Save huoxudong125/dfd018bd8315c74ed136228b6f2c98cd to your computer and use it in GitHub Desktop.
ApiLogHandler
This file contains 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 ApiLogHandler : DelegatingHandler | |
{ | |
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | |
{ | |
var apiLogEntry = CreateApiLogEntryWithRequestData(request); | |
if (request.Content != null) | |
{ | |
await request.Content.ReadAsStringAsync() | |
.ContinueWith(task => | |
{ | |
apiLogEntry.RequestContentBody = task.Result; | |
}, cancellationToken); | |
} | |
return await base.SendAsync(request, cancellationToken) | |
.ContinueWith(task => | |
{ | |
var response = task.Result; | |
// Update the API log entry with response info | |
apiLogEntry.ResponseStatusCode = (int)response.StatusCode; | |
apiLogEntry.ResponseTimestamp = DateTime.Now; | |
if (response.Content != null) | |
{ | |
apiLogEntry.ResponseContentBody = response.Content.ReadAsStringAsync().Result; | |
apiLogEntry.ResponseContentType = response.Content.Headers.ContentType.MediaType; | |
apiLogEntry.ResponseHeaders = SerializeHeaders(response.Content.Headers); | |
} | |
// TODO: Save the API log entry to the database | |
return response; | |
}, cancellationToken); | |
} | |
private ApiLogEntry CreateApiLogEntryWithRequestData(HttpRequestMessage request) | |
{ | |
var context = ((HttpContextBase)request.Properties["MS_HttpContext"]); | |
var routeData = request.GetRouteData(); | |
return new ApiLogEntry | |
{ | |
Application = "[insert-calling-app-here]", | |
User = context.User.Identity.Name, | |
Machine = Environment.MachineName, | |
RequestContentType = context.Request.ContentType, | |
RequestRouteTemplate = routeData.Route.RouteTemplate, | |
RequestRouteData = SerializeRouteData(routeData), | |
RequestIpAddress = context.Request.UserHostAddress, | |
RequestMethod = request.Method.Method, | |
RequestHeaders = SerializeHeaders(request.Headers), | |
RequestTimestamp = DateTime.Now, | |
RequestUri = request.RequestUri.ToString() | |
}; | |
} | |
private string SerializeRouteData(IHttpRouteData routeData) | |
{ | |
return JsonConvert.SerializeObject(routeData, Formatting.Indented); | |
} | |
private string SerializeHeaders(HttpHeaders headers) | |
{ | |
var dict = new Dictionary<string, string>(); | |
foreach (var item in headers.ToList()) | |
{ | |
if (item.Value != null) | |
{ | |
var header = String.Empty; | |
foreach (var value in item.Value) | |
{ | |
header += value + " "; | |
} | |
// Trim the trailing space and add item to the dictionary | |
header = header.TrimEnd(" ".ToCharArray()); | |
dict.Add(item.Key, header); | |
} | |
} | |
return JsonConvert.SerializeObject(dict, Formatting.Indented); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment