public async Task InvokeAsync(HttpContext context, RequestDelegate next)
 {
     try
     {
         await next(context);
     }
     catch (HandledException hex)
     {
         context.Response.StatusCode = (int)hex.StatusCode;
     }
     catch (Exception ex)
     {
         logger.Write(LogLevel.Error, "unhandled exception", new { error = ex.Message, stacktrace = ex.StackTrace });
         context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
     }
 }
Esempio n. 2
0
        public async Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            Stopwatch watch = new Stopwatch();

            watch.Restart();

            context.Request.EnableBuffering();

            //request scope
            var requestBuffer = new MemoryStream();
            await context.Request.BodyReader.CopyToAsync(requestBuffer);

            context.Request.Body.Position = 0;

            //response scope
            var clientResponseStream = context.Response.Body;
            var responseBuffer       = new MemoryStream();

            context.Response.Body = responseBuffer;

            //process api action
            await next(context);

            responseBuffer.Position = 0;
            await responseBuffer.CopyToAsync(clientResponseStream);

            //await clientResponseStream.WriteAsync(response, 0, response.Length);

            watch.Stop();

            var result = new ApiInvokeResult()
            {
                StatusCode          = context.Response.StatusCode,
                ElapsedMilliseconds = watch.ElapsedMilliseconds,
                RequestBody         = encoding.GetString(requestBuffer.ToArray()),
                ResponseBody        = encoding.GetString(responseBuffer.ToArray()),
            };

            logger.Write(Microsoft.Extensions.Logging.LogLevel.Information, "api log", result);
        }
Esempio n. 3
0
 public ValuesController(JsonLogger logger)
 {
     logger.Write(LogLevel.Debug, "values controller created", new { });
 }