예제 #1
0
        public async Task InvokeAsync(HttpContext context)
        {
            if (AppSettingsHelper.GetContent("Middleware", "IPLog", "Enabled").ObjectToBool())
            {
                // 过滤,只有接口
                if (context.Request.Path.Value.Contains("api") || context.Request.Path.Value.Contains("Api"))
                {
                    context.Request.EnableBuffering();

                    try
                    {
                        // 存储请求数据
                        var dt          = DateTime.Now;
                        var request     = context.Request;
                        var requestInfo = JsonConvert.SerializeObject(new RequestInfo()
                        {
                            Ip       = GetClientIP(context),
                            Url      = request.Path.ObjectToString().TrimEnd('/').ToLower(),
                            Datetime = dt.ToString("yyyy-MM-dd HH:mm:ss"),
                            Date     = dt.ToString("yyyy-MM-dd"),
                            Week     = GetWeek(),
                        });

                        if (!string.IsNullOrEmpty(requestInfo))
                        {
                            Parallel.For(0, 1, e =>
                            {
                                LogLockHelper.OutSql2Log("RequestIpInfoLog" + dt.ToString("yyyy-MM-dd"), new string[] { requestInfo + "," }, false);
                            });

                            request.Body.Position = 0;
                        }

                        await _next(context);
                    }
                    catch (Exception)
                    {
                    }
                }
                else
                {
                    await _next(context);
                }
            }
            else
            {
                await _next(context);
            }
        }
예제 #2
0
        private void ResponseDataLog(HttpResponse response, MemoryStream ms)
        {
            ms.Position = 0;
            var ResponseBody = new StreamReader(ms).ReadToEnd();
            // 去除 Html
            var reg    = "<[^>]+>";
            var isHtml = Regex.IsMatch(ResponseBody, reg);

            if (!string.IsNullOrEmpty(ResponseBody))
            {
                Parallel.For(0, 1, e =>
                {
                    LogLockHelper.OutSql2Log("RequestResponseLog" + DateTime.Now.ToString("yyyy-MM-dd"), new string[] { "Response Data:", ResponseBody });
                });
            }
        }
예제 #3
0
        private async Task RequestDataLog(HttpContext context)
        {
            var request = context.Request;
            var sr      = new StreamReader(request.Body);

            var content = $" QueryData:{request.Path + request.QueryString}\r\n BodyData:{await sr.ReadToEndAsync()}";

            if (!string.IsNullOrEmpty(content))
            {
                Parallel.For(0, 1, e =>
                {
                    LogLockHelper.OutSql2Log("RequestResponseLog" + DateTime.Now.ToString("yyyy-MM-dd"), new string[] { "Request Data:", content });
                });

                request.Body.Position = 0;
            }
        }