예제 #1
0
        /// <summary>
        /// 日志事件
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task InvokeAsync(HttpContext context)
        {
            //判断是否响应成功
            if (context.Response.StatusCode != (int)HttpStatusCode.OK)
            {
                return;
            }
            var requestPath = context.Request.PathBase + context.Request.Path;

            if (requestPath.Value != null && requestPath.Value.Equals("/swagger/v1/swagger.json"))
            {
                await _next(context);
            }
            else
            {
                //开始记录日志
                var serviceProvider = context.RequestServices;
                var logger          = serviceProvider.GetRequiredService <ILogger <ASFRequestLoggerMiddleware> >();
                try
                {
                    if (context.Items.ContainsKey("asf_api_parmission"))
                    {
                        if (context.Items["asf_api_parmission"] is Api api)
                        {
                            //判断是否需要记录日志
                            if (api.IsLogger != null && (Status)api.IsLogger == Status.Yes)
                            {
                                await ASFRequestLogger.GetInstance(context, api, this._next).Record();
                            }
                            else
                            {
                                await _next(context);
                            }
                        }
                        else
                        {
                            logger.LogError($"HttpContext.Items[\"asf_api_parmission\"] is not equal to {typeof(Api)}");
                            await Task.CompletedTask;
                            // return;
                        }
                    }

                    await Task.CompletedTask;
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, "ASF Logging");
                    await _next(context);
                }
            }
        }
예제 #2
0
        public static async Task Invoke(DownstreamContext context, Func <Task> next)
        {
            await next.Invoke();

            //判断是否响应成功
            if (context.DownstreamResponse == null ||
                context.DownstreamResponse.StatusCode != HttpStatusCode.OK)
            {
                return;
            }

            //开始记录日志
            var serviceProvider = context.HttpContext.RequestServices;
            var logger          = serviceProvider.GetRequiredService <ILogger <ASFRequestLoggerMiddleware> >();

            try
            {
                if (context.HttpContext.Items.ContainsKey("asf_parmission"))
                {
                    if (context.HttpContext.Items["asf_parmission"] is Permission per)
                    {
                        //判断是否需要记录日志
                        if (per.IsLogger)
                        {
                            await ASFRequestLogger.Record(context, per);
                        }
                    }
                    else
                    {
                        logger.LogError($"HttpContext.Items[\"asf_parmission\"] is not equal to {typeof(Permission)}");

                        return;
                    }
                }
                return;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "ASF Logging");
                await next.Invoke();

                return;
            }
        }