Skip to content
Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
135 lines (118 sloc) 4.55 KB
using AspNetCoreApiLoggingSample.Data;
using AspNetCoreApiLoggingSample.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Internal;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AspNetCoreApiLoggingSample.Middleware
{
public class ApiLoggingMiddleware
{
private readonly RequestDelegate _next;
private ApiLogService _apiLogService;
public ApiLoggingMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext httpContext, ApiLogService apiLogService)
{
try
{
_apiLogService = apiLogService;
var request = httpContext.Request;
if (request.Path.StartsWithSegments(new PathString("/api")))
{
var stopWatch = Stopwatch.StartNew();
var requestTime = DateTime.UtcNow;
var requestBodyContent = await ReadRequestBody(request);
var originalBodyStream = httpContext.Response.Body;
using (var responseBody = new MemoryStream())
{
var response = httpContext.Response;
response.Body = responseBody;
await _next(httpContext);
stopWatch.Stop();
string responseBodyContent = null;
responseBodyContent = await ReadResponseBody(response);
await responseBody.CopyToAsync(originalBodyStream);
await SafeLog(requestTime,
stopWatch.ElapsedMilliseconds,
response.StatusCode,
request.Method,
request.Path,
request.QueryString.ToString(),
requestBodyContent,
responseBodyContent);
}
}
else
{
await _next(httpContext);
}
}
catch (Exception ex)
{
await _next(httpContext);
}
}
private async Task<string> ReadRequestBody(HttpRequest request)
{
request.EnableRewind();
var buffer = new byte[Convert.ToInt32(request.ContentLength)];
await request.Body.ReadAsync(buffer, 0, buffer.Length);
var bodyAsText = Encoding.UTF8.GetString(buffer);
request.Body.Seek(0, SeekOrigin.Begin);
return bodyAsText;
}
private async Task<string> ReadResponseBody(HttpResponse response)
{
response.Body.Seek(0, SeekOrigin.Begin);
var bodyAsText = await new StreamReader(response.Body).ReadToEndAsync();
response.Body.Seek(0, SeekOrigin.Begin);
return bodyAsText;
}
private async Task SafeLog(DateTime requestTime,
long responseMillis,
int statusCode,
string method,
string path,
string queryString,
string requestBody,
string responseBody)
{
if (path.ToLower().StartsWith("/api/login"))
{
requestBody = "(Request logging disabled for /api/login)";
responseBody = "(Response logging disabled for /api/login)";
}
if (requestBody.Length > 100)
{
requestBody = $"(Truncated to 100 chars) {requestBody.Substring(0, 100)}";
}
if (responseBody.Length > 100)
{
responseBody = $"(Truncated to 100 chars) {responseBody.Substring(0, 100)}";
}
if (queryString.Length > 100)
{
queryString = $"(Truncated to 100 chars) {queryString.Substring(0, 100)}";
}
await _apiLogService.Log(new ApiLogItem
{
RequestTime = requestTime,
ResponseMillis = responseMillis,
StatusCode = statusCode,
Method = method,
Path = path,
QueryString = queryString,
RequestBody = requestBody,
ResponseBody = responseBody
});
}
}
}
You can’t perform that action at this time.