Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public Task InvokeAsync(HttpContext context)
        {
            var _apiLogIsEnable = _cfg.GetValue <bool>("ApiLog:IsEnable");

            if (_apiLogIsEnable)
            {
                context.Request.EnableRewind();
            }
            // Start the Timer using Stopwatch
            var watch = new Stopwatch();

            watch.Start();
            context.Response.OnStarting(() =>
            {
                // Stop the timer information and calculate the time
                watch.Stop();

                //是否启用访问日志功能,默认为不启用
                if (!_apiLogIsEnable)
                {
                    // Add the Response time information in the Response headers.
                    context.Response.Headers[RESPONSE_HEADER_RESPONSE_TIME] = watch.ElapsedMilliseconds.ToString();
                }
                else
                {
                    _apiLogService.DataSave(context, watch.ElapsedMilliseconds);
                }
                return(Task.CompletedTask);
            });
            // Call the next delegate/middleware in the pipeline
            return(this._next(context));
        }
Exemplo n.º 2
0
        public Task Invoke(HttpContext context)
        {
            var watch = new Stopwatch();

            watch.Start();
            context.Response.OnStarting(() =>
            {
                var isLog = _configration.GetValue <bool>("ApiLog:IsEnable");
                if (isLog)
                {
                    watch.Stop();
                    _apiLogService.DataSave(context, watch.ElapsedMilliseconds);
                }
                return(Task.CompletedTask);
            });

            return(this._next(context));
        }