Beispiel #1
0
        public async Task InsertHttpRequestLogAsync(HttpRequestLog request)
        {
            using (var conn = new SqlConnection(_connectionString))
            {
                await conn.OpenAsync();
                var cmd = new SqlCommand("uspInsertRequestLog", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.AddWithValue("@Id", request.Id);
                cmd.Parameters.AddWithValue("@UserName", request.UserName);
                cmd.Parameters.AddWithValue("@UserIpAddress", request.UserIpAddress);
                cmd.Parameters.AddWithValue("@HttpAction", request.HttpAction);
                cmd.Parameters.AddWithValue("@RequestUrl", request.RequestUrl);
                cmd.Parameters.AddWithValue("@RequestHeader", request.RequestHeader);
                cmd.Parameters.AddWithValue("@RequestBody", request.RequestBody);
                cmd.Parameters.AddWithValue("@UserAgent", request.UserAgent);
                cmd.Parameters.AddWithValue("@DeviceInfo", request.DeviceInfo);
                cmd.Parameters.AddWithValue("@BrowserInfo", request.BrowserInfo);
                cmd.Parameters.AddWithValue("@IsAnonymous", request.IsAnonymous);
                cmd.Parameters.AddWithValue("@IsAuthenticated", request.IsAuthenticated);
                cmd.Parameters.AddWithValue("@IsGuest", request.IsGuest);
                cmd.Parameters.AddWithValue("@IsSystem", request.IsSystem);
                cmd.Parameters.AddWithValue("@RequestTimeStamp", request.RequestTimeStamp);

                await cmd.ExecuteNonQueryAsync();
                conn.Close();
            }
        }
Beispiel #2
0
        public async Task FinalizeRequestLogAsync(HttpRequestLog request)
        {
            using (var conn = new SqlConnection(_connectionString))
            {
                await conn.OpenAsync();
                var cmd = new SqlCommand("uspFinalizeRequestLog", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.AddWithValue("@Id", request.Id);
                cmd.Parameters.AddWithValue("@Response", request.Response);
                cmd.Parameters.AddWithValue("@ResponseBody", request.ResponseBody);
                cmd.Parameters.AddWithValue("@ResponseTimeStamp", request.ResposneTimeStamp);
                cmd.Parameters.AddWithValue("@RequestTotalTime", request.RequestTotalTime);

                await cmd.ExecuteNonQueryAsync();
                conn.Close();
            }
        }
        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            var requestInfo = $"{request.Method} {request.RequestUri}";

            var req = ((dynamic) request.Properties["MS_HttpContext"]).Request;

            var requestMessage = await request.Content.ReadAsByteArrayAsync();

            var requestTime = DateTime.Now;

            var user = string.Empty;

            if (req.LogonUserIdentity.IsAuthenticated)
            {
                var principal = (ClaimsPrincipal) Thread.CurrentPrincipal;
                user = RequestHelpers.GetUserId(principal);
            }

            var headers = new StringBuilder();
            if (request.Headers != null)
            {
                foreach (var header in request.Headers)
                {
                    var val = new StringBuilder();
                    foreach (var keyValue in header.Value)
                    {
                        val.Append($"{keyValue},");
                    }

                    headers.AppendLine($"{header.Key} | {val}");
                }
            }

            ClientInfo ua = null;
            if (req != null && req.UserAgent != null)
            {
                ua = Parser.GetDefault().Parse(req.UserAgent);
            }

            var httpRequestLog = new HttpRequestLog
            {
                UserName = user,
                UserIpAddress = req.UserHostAddress,
                UserAgent = ua == null ? "" : ua.UserAgent.ToString(),
                DeviceInfo = ua == null ? "" : ua.Device.Family,
                BrowserInfo = ua == null ? "" : ua.UserAgent.Family,
                IsAnonymous = req.LogonUserIdentity.IsAnonymous,
                IsAuthenticated = req.LogonUserIdentity.IsAuthenticated,
                IsGuest = req.LogonUserIdentity.IsGuest,
                IsSystem = req.LogonUserIdentity.IsSystem,
                HttpAction = request.Method.ToString(),
                RequestUrl = request.RequestUri.ToString(),
                RequestHeader = headers.ToString(),
                RequestBody = RequestHelpers.GetRequestBody(request),
                RequestTimeStamp = requestTime
            };

            await _repo.InsertHttpRequestLogAsync(httpRequestLog);

            var response = await base.SendAsync(request, cancellationToken);

            byte[] responseMessage;

            if (response.IsSuccessStatusCode)
                responseMessage = await response.Content.ReadAsByteArrayAsync();
            else
                responseMessage = Encoding.UTF8.GetBytes(response.ReasonPhrase);

            var responseTime = DateTime.Now;

            httpRequestLog.Response = Convert.ToInt32(response.StatusCode);
            httpRequestLog.ResponseBody = Encoding.UTF8.GetString(responseMessage);
            httpRequestLog.ResposneTimeStamp = responseTime;
            httpRequestLog.RequestTotalTime = responseTime - requestTime;

            await _repo.FinalizeRequestLogAsync(httpRequestLog);

            return response;
        }