Exemplo n.º 1
0
        public async Task InvokeAsync(HttpContext context)
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            _streamReaderRequest = new StreamReader(context.Request.Body);
            string remoteAddress = context.Connection.RemoteIpAddress.ToString();

            if (remoteAddress == "::1")
            {
                remoteAddress = "127.0.0.1";
            }
            string forwarderAddress = context.Request.Headers["X-Forwarded-For"];
            string requestStr       = await _streamReaderRequest.ReadToEndAsync();

            StringBuilder sb   = new StringBuilder();
            string        host = context.Request.Host.ToString();

            sb.Append($"1.{context.Request.Host.ToString()}/n/r");                 //主机地址
            string uri = UriHelper.GetDisplayUrl(context.Request);

            sb.Append($"2.{UriHelper.GetDisplayUrl(context.Request)}/n/r");        //请求url
            var traceIdentifier = context.TraceIdentifier;

            sb.Append($"3.{context.TraceIdentifier}/n/r");                   //请求唯一标识
            sb.Append($"4.Token暂时没有");                                       //token
            sb.Append($"5.{requestStr}/n/r");                                //请求body
            sb.Append($"6.用户id或者标识/n/r");                                    //用户id或者标识
            DateTime timeCreate            = DateTime.Now;                   //请求时间
            var      headers               = BuildHeader(context.Request.Headers);
            var      newResponseBodyStream = new MemoryStream();

            context.Response.Body = newResponseBodyStream;                       //绝对不能加,加了就获取不到responsel了,但是不用日志就取不到response,不加的话,日志获取不到response

            await _next(context);                                                //继续执行中间件

            newResponseBodyStream.Seek(0, SeekOrigin.Begin);
            var responseBodyText = new StreamReader(newResponseBodyStream, Encoding.UTF8).ReadToEnd();   //获取请求的response

            stopwatch.Stop();
            var dissipate = stopwatch.ElapsedMilliseconds;
            HttpRequestLogDAL httpRequestLog = new HttpRequestLogDAL();
            var insertInfo = new HttpRequestLog()
            {
                RemoteAddress    = remoteAddress,
                ForwarderAddress = forwarderAddress,
                Host             = host,
                Uri            = uri,
                TraceIdentiier = traceIdentifier,
                Token          = null,
                RequestBody    = requestStr,
                TimeCreate     = timeCreate,
                Headers        = headers,
                ResponseBody   = responseBodyText,
                Dissipate      = dissipate
            };
            await httpRequestLog.InsertAsync(insertInfo);
        }
Exemplo n.º 2
0
        //private void WritLog(HttpContext context, string body)
        //{
        //    var request = context.Request;
        //    try
        //    {
        //        Task.Factory.StartNew(() =>
        //        {
        //            LogMode logMode = new LogMode
        //            {
        //                Body = body,
        //                CententLength = request.ContentLength,
        //                CententType = request.ContentType,
        //                Headers = JsonConvert.SerializeObject(request.Headers),
        //                Host = request.Host.Host,
        //                Method = request.Method,
        //                Path = request.Path,
        //                Query = JsonConvert.SerializeObject(request.Query)
        //            };          // 记录日志
        //           // ExceptionlessClient.Default.CreateLog(JsonConvert.SerializeObject(logMode)).AddTags(context.TraceIdentifier).Submit();
        //        });
        //    }
        //    catch (Exception ex)
        //    {         //记录日志
        //         //ex.ToExceptionless().Submit();
        //    }

        //}

        private async Task WriteLog(HttpRequestLog httpRequestLog)
        {
            HttpRequestLogDAL httpRequestLogDAL = new HttpRequestLogDAL();
            await httpRequestLogDAL.InsertAsync(httpRequestLog);
        }