Exemplo n.º 1
0
        /// <summary>
        /// 服务端处理
        /// </summary>
        /// <typeparam name="TRequest">请求类型</typeparam>
        /// <typeparam name="TResponse">响应类型</typeparam>
        /// <param name="request">请求对象</param>
        /// <param name="context">服务回调上下文</param>
        /// <param name="continuation">服务方法</param>
        /// <returns>响应任务</returns>
        public override Task <TResponse> UnaryServerHandler <TRequest, TResponse>(TRequest request, ServerCallContext context, UnaryServerMethod <TRequest, TResponse> continuation)
        {
            var stop = new Stopwatch();

            stop.Start();
            var path = context.GetHttpContext().Request.Path.Value;
            var re   = continuation(request, context);

            stop.Stop();

            var    msg     = $"GRpc请求:{path} 耗时:{stop.ElapsedMilliseconds}ms";
            string eventId = null;

            if (theOperation != null)
            {
                if (string.IsNullOrWhiteSpace(theOperation.EventId))
                {
                    theOperation.EventId = context.GetHttpContext().Request.GetEventId();
                }
                eventId = theOperation.EventId;
            }
            switch (options.LogLevel)
            {
            case LogLevelEnum.TRACE:
                _ = log.TraceAsync(msg, null, "GRpcRequestLogInterceptor", eventId: eventId, path);

                break;

            case LogLevelEnum.DEBUG:
                _ = log.DebugAsync(msg, null, "GRpcRequestLogInterceptor", eventId: eventId, path);

                break;

            case LogLevelEnum.WRAN:
                _ = log.WranAsync(msg, null, "GRpcRequestLogInterceptor", eventId: eventId, path);

                break;

            case LogLevelEnum.INFO:
                _ = log.InfoAsync(msg, null, "GRpcRequestLogInterceptor", eventId: eventId, path);

                break;

            case LogLevelEnum.ERROR:
                _ = log.ErrorAsync(msg, null, "GRpcRequestLogInterceptor", eventId: eventId, path);

                break;

            case LogLevelEnum.FATAL:
                _ = log.FatalAsync(msg, null, "GRpcRequestLogInterceptor", eventId: eventId, path);

                break;
            }

            return(re);
        }
        /// <summary>
        /// 执行
        /// </summary>
        /// <param name="context">http上下文</param>
        /// <returns>任务</returns>
        public async Task InvokeAsync(HttpContext context)
        {
            var stop = new Stopwatch();

            stop.Start();
            var path = context.Request.Path.Value.ToLower();

            await next(context);

            stop.Stop();

            var msg = $"请求:{path},耗时:{stop.ElapsedMilliseconds}ms";

            switch (options.LogLevel)
            {
            case LogLevelEnum.TRACE:
                _ = log.TraceAsync(msg, null, "RequestLogMiddleware", path);

                break;

            case LogLevelEnum.DEBUG:
                _ = log.DebugAsync(msg, null, "RequestLogMiddleware", path);

                break;

            case LogLevelEnum.WRAN:
                _ = log.WranAsync(msg, null, "RequestLogMiddleware", path);

                break;

            case LogLevelEnum.INFO:
                _ = log.InfoAsync(msg, null, "RequestLogMiddleware", path);

                break;

            case LogLevelEnum.ERROR:
                _ = log.ErrorAsync(msg, null, "RequestLogMiddleware", path);

                break;

            case LogLevelEnum.FATAL:
                _ = log.FatalAsync(msg, null, "RequestLogMiddleware", path);

                break;
            }
        }
        /// <summary>
        /// 执行
        /// </summary>
        /// <param name="context">http上下文</param>
        /// <returns>任务</returns>
        public async Task InvokeAsync(HttpContext context)
        {
            var routeValue = context.Request.RouteValues;
            var routes     = routeValue.GetControllerAction();

            // 过滤掉非控制器(排除掉GRpc)
            if (routes.IsNullOrLength0() && !GRpcChannelUtil.IsRequestGRpc(context.Request.ContentType))
            {
                await next(context);

                return;
            }

            var stop = new Stopwatch();

            stop.Start();
            var path = context.Request.Path.Value.ToLower();

            await next(context);

            stop.Stop();

            var    msg = new StringBuilder($"请求:{path} method:{context.Request.Method} ");
            string controller = null, action = null;

            if (routes != null && routes.Length == 2)
            {
                controller = routes[0];
                action     = routes[1];
                msg.AppendFormat("controller:{0},action:{1}.", controller, action);
            }

            msg.Append($"耗时:{stop.ElapsedMilliseconds}ms");
            var    msgStr  = msg.ToString();
            string eventId = theOperation != null ? theOperation.EventId : null;

            switch (options.LogLevel)
            {
            case LogLevelEnum.TRACE:
                _ = log.TraceAsync(msgStr, null, "RequestLogMiddleware", eventId: eventId, path, controller, action);

                break;

            case LogLevelEnum.DEBUG:
                _ = log.DebugAsync(msgStr, null, "RequestLogMiddleware", eventId: eventId, path, controller, action);

                break;

            case LogLevelEnum.WRAN:
                _ = log.WranAsync(msgStr, null, "RequestLogMiddleware", eventId: eventId, path, controller, action);

                break;

            case LogLevelEnum.INFO:
                _ = log.InfoAsync(msgStr, null, "RequestLogMiddleware", eventId: eventId, path, controller, action);

                break;

            case LogLevelEnum.ERROR:
                _ = log.ErrorAsync(msgStr, null, "RequestLogMiddleware", eventId: eventId, path, controller, action);

                break;

            case LogLevelEnum.FATAL:
                _ = log.FatalAsync(msgStr, null, "RequestLogMiddleware", eventId: eventId, path, controller, action);

                break;
            }
        }