Beispiel #1
0
        protected RequestLogEntry CreateEntry(IRequest request, object requestDto, object response, TimeSpan requestDuration, Type requestType)
        {
            var entry = new RequestLogEntry
            {
                Id              = Interlocked.Increment(ref requestId),
                DateTime        = CurrentDateFn(),
                RequestDuration = requestDuration,
            };

            if (request != null)
            {
                entry.HttpMethod        = request.Verb;
                entry.AbsoluteUri       = request.AbsoluteUri;
                entry.PathInfo          = request.PathInfo;
                entry.IpAddress         = request.UserHostAddress;
                entry.ForwardedFor      = request.Headers[HttpHeaders.XForwardedFor];
                entry.Referer           = request.Headers[HttpHeaders.Referer];
                entry.Headers           = request.Headers.ToDictionary();
                entry.UserAuthId        = request.GetItemStringValue(HttpHeaders.XUserAuthId);
                entry.Items             = SerializableItems(request.Items);
                entry.Session           = EnableSessionTracking ? request.GetSession() : null;
                entry.StatusCode        = request.Response.StatusCode;
                entry.StatusDescription = request.Response.StatusDescription;

                var isClosed = request.Response.IsClosed;
                if (!isClosed)
                {
                    entry.UserAuthId = request.GetItemOrCookie(HttpHeaders.XUserAuthId);
                    entry.SessionId  = request.GetSessionId();
                }

                if (HideRequestBodyForRequestDtoTypes != null &&
                    requestType != null &&
                    !HideRequestBodyForRequestDtoTypes.Contains(requestType))
                {
                    entry.RequestDto = requestDto;

                    if (!isClosed)
                    {
                        entry.FormData = request.FormData.ToDictionary();
                    }

                    if (EnableRequestBodyTracking)
                    {
#if NETSTANDARD2_0
                        // https://forums.servicestack.net/t/unexpected-end-of-stream-when-uploading-to-aspnet-core/6478/6
                        if (!request.ContentType.MatchesContentType(MimeTypes.MultiPartFormData))
                        {
                            entry.RequestBody = request.GetRawBody();
                        }
#else
                        entry.RequestBody = request.GetRawBody();
#endif
                    }
                }
            }

            if (!response.IsErrorResponse())
            {
                if (EnableResponseTracking)
                {
                    entry.ResponseDto = response.GetResponseDto();
                }
            }
            else
            {
                if (EnableErrorTracking)
                {
                    entry.ErrorResponse = ToSerializableErrorResponse(response);

                    if (response is IHttpError httpError)
                    {
                        entry.StatusCode        = (int)httpError.StatusCode;
                        entry.StatusDescription = httpError.StatusDescription;
                    }

                    if (response is Exception exception)
                    {
                        if (exception.InnerException != null)
                        {
                            exception = exception.InnerException;
                        }

                        entry.ExceptionSource = exception.Source;
                        entry.ExceptionData   = exception.Data;
                    }
                }
            }

            return(entry);
        }
        protected SeqRequestLogEntry CreateEntry(
            IRequest request,
            object requestDto,
            object response,
            TimeSpan requestDuration,
            Type requestType)
        {
            var requestLogEntry = new SeqRequestLogEntry();

            requestLogEntry.Timestamp       = DateTime.UtcNow.ToString("o");
            requestLogEntry.MessageTemplate = "HTTP {HttpMethod} {PathInfo} responded {StatusCode} in {ElapsedMilliseconds}ms";
            requestLogEntry.Properties.Add("IsRequestLog", "True"); // Used for filtering requests easily
            requestLogEntry.Properties.Add("SourceContext", "ServiceStack.Seq.RequestLogsFeature");
            requestLogEntry.Properties.Add("ElapsedMilliseconds", requestDuration.TotalMilliseconds);
            requestLogEntry.Properties.Add("RequestCount", Interlocked.Increment(ref requestId).ToString());
            requestLogEntry.Properties.Add("ServiceName", HostContext.AppHost.ServiceName);

            if (request != null)
            {
                requestLogEntry.Properties.Add("HttpMethod", request.Verb);
                requestLogEntry.Properties.Add("AbsoluteUri", request.AbsoluteUri);
                requestLogEntry.Properties.Add("PathInfo", request.PathInfo);
                requestLogEntry.Properties.Add("IpAddress", request.UserHostAddress);
                requestLogEntry.Properties.Add("ForwardedFor", request.Headers[HttpHeaders.XForwardedFor]);
                requestLogEntry.Properties.Add("Referer", request.Headers[HttpHeaders.Referer]);
                requestLogEntry.Properties.Add("Session", EnableSessionTracking ? request.GetSession(false) : null);
                requestLogEntry.Properties.Add("Items", request.Items.WithoutDuplicates());
                requestLogEntry.Properties.Add("StatusCode", request.Response?.StatusCode);
                requestLogEntry.Properties.Add("StatusDescription", request.Response?.StatusDescription);
                requestLogEntry.Properties.Add("ResponseStatus", request.Response?.GetResponseStatus());
            }

            var isClosed = request.Response.IsClosed;

            if (!isClosed)
            {
                requestLogEntry.Properties.Add("UserAuthId", request.GetItemOrCookie(HttpHeaders.XUserAuthId));
                requestLogEntry.Properties.Add("SessionId", request.GetSessionId());
            }

            if (HideRequestBodyForRequestDtoTypes != null &&
                requestType != null &&
                !HideRequestBodyForRequestDtoTypes.Contains(requestType))
            {
                requestLogEntry.Properties.Add("RequestDto", requestDto);
                if (request != null)
                {
                    if (!isClosed)
                    {
                        requestLogEntry.Properties.Add("FormData", request.FormData.ToDictionary());
                    }

                    if (EnableRequestBodyTracking)
                    {
                        requestLogEntry.Properties.Add("RequestBody", request.GetRawBody());
                    }
                }
            }

            if (!response.IsErrorResponse())
            {
                if (EnableResponseTracking)
                {
                    requestLogEntry.Properties.Add("ResponseDto", response);
                }
            }
            else if (EnableErrorTracking)
            {
                var errorResponse = response as IHttpError;
                if (errorResponse != null)
                {
                    requestLogEntry.Level = errorResponse.StatusCode >= HttpStatusCode.BadRequest &&
                                            errorResponse.StatusCode < HttpStatusCode.InternalServerError
                                                ? "Warning"
                                                : "Error";
                    requestLogEntry.Properties["StatusCode"] = (int)errorResponse.StatusCode;
                    requestLogEntry.Properties.Add("ErrorCode", errorResponse.ErrorCode);
                    requestLogEntry.Properties.Add("ErrorMessage", errorResponse.Message);
                    requestLogEntry.Properties.Add("StackTrace", errorResponse.StackTrace);
                }

                var ex = response as Exception;
                if (ex != null)
                {
                    if (ex.InnerException != null)
                    {
                        requestLogEntry.Exception = ex.InnerException.ToString();
                        requestLogEntry.Properties.Add("ExceptionSource", ex.InnerException.Source);
                        requestLogEntry.Properties.Add("ExceptionData", ex.InnerException.Data);
                    }
                    else
                    {
                        requestLogEntry.Exception = ex.ToString();
                    }
                }
            }

            if (AppendProperties != null)
            {
                foreach (var kvPair in AppendProperties?.Invoke(request, requestDto, response, requestDuration).Safe())
                {
                    requestLogEntry.Properties.GetOrAdd(kvPair.Key, key => kvPair.Value);
                }
            }

            foreach (var header in request.Headers.ToDictionary())
            {
                if (!requestLogEntry.Properties.ContainsValue(header.Value))
                {
                    requestLogEntry.Properties.Add($"Header-{header.Key}", header.Value);
                }
            }

            return(requestLogEntry);
        }