示例#1
0
 public static iAFWebHost.Models.RequestLogModel Map(Entities.RequestLog entity)
 {
     iAFWebHost.Models.RequestLogModel model = new RequestLogModel();
     model.Id         = entity.Id;
     model.DT         = entity.DT;
     model.Raw        = entity.Raw;
     model.Referrer   = entity.Referrer;
     model.RemoteIP   = entity.RemoteIP;
     model.RequestUrl = entity.RequestUrl;
     return(model);
 }
示例#2
0
        public async Task <RequestLogModel> RequestIndiactor(HttpContext httpContext)
        {
            // TODO. ASK HOW TO FILL BELOW FIELDS
            // ActionTypeId - not now
            // EntityChanges - not now
            // QueryString - route parameter
            var requestAddress = httpContext.Request.Path;

            _stopwatch.Start();
            var request = new Entities.RequestLog
            {
                IpAddress = httpContext.Connection.RemoteIpAddress.ToString(),
                Path      = requestAddress,
                Method    = httpContext.Request.Method,
                StartTime = DateTime.Now,
            };

            if (httpContext.Request?.Body != null && !string.IsNullOrWhiteSpace(httpContext.Request.ContentType) &&
                !httpContext.Request.ContentType.StartsWith("multipart/form-data", StringComparison.InvariantCultureIgnoreCase))
            {
                #region Get Request Body
                request.Body = await new StreamReader(httpContext.Request.Body).ReadToEndAsync();
                var injectedRequestStream = new MemoryStream();
                var bytesToWrite          = Encoding.UTF8.GetBytes(request.Body);
                injectedRequestStream.Write(bytesToWrite, 0, bytesToWrite.Length);
                injectedRequestStream.Seek(0, SeekOrigin.Begin);
                httpContext.Request.Body = injectedRequestStream;
                #endregion
            }

            request.RequestSize = httpContext.Request.ContentLength;
            if (!IgnoreRequest(requestAddress))
            {
                _dbContext.RequestLogs.Add(request);
                await _dbContext.SaveChangesAsync();
            }
            return(new RequestLogModel
            {
                Id = request.Id,
                Body = request.Body,
                IpAddress = request.IpAddress,
                Method = request.Method,
                Path = request.Path,
                StartTime = request.StartTime,
                UserId = request.UserId,
                SavedLog = request,
            });
        }