コード例 #1
0
        /// <summary>
        /// Invokes the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="appLogger">Application logger instance</param>
        /// <param name="log">The log.</param>
        /// <returns></returns>
        public async Task Invoke(HttpContext context, IAppLogger appLogger, AppLog log)
        {
            if (IsAssetRequest(context.Request.Path.Value))
            {
                await _next(context);

                return;
            }
            var stopWatch = Stopwatch.StartNew();

            log.Request = GetBodyString(context.Request);
            var originalBodyStream = context.Response.Body;

            using (var responseBody = new MemoryStream())
            {
                context.Response.Body  = responseBody;
                log.ApplicationVersion = _settings.AppVersion;
                log.DeviceInfo         = context.Request.Headers["DeviceInfo"].FirstOrDefault();
                log.DeviceIP           = context.Request.Headers["DeviceIP"].FirstOrDefault();
                log.OSType             = context.Request.Headers["OSType"].FirstOrDefault();
                log.HostIP             = GetIpAddress();
                log.RequestMethod      = context.Request.Method;
                log.RequestURL         = context.Request.Path;
                log.RequestHeaders     = JsonConvert.SerializeObject(context.Request.Headers);
                await _next(context);

                var response = await FormatResponse(context.Response);

                log.HttpStatus   = context.Response.StatusCode;
                log.Response     = log.HttpStatus == 200 ? "Success" : response;
                log.ResponseTime = stopWatch.Elapsed.Milliseconds;
                context.Response.Headers.Add("Reference", log.ReferenceNumber.ToString());
                await appLogger.Commit(log);

                await responseBody.CopyToAsync(originalBodyStream);
            }
        }