private void BufferedLogEntries(SeqRequestLogEntry entry) { // TODO add buffering to logging for perf // scope to force json camel casing off using (JsConfig.With(emitCamelCaseNames: false)) { eventsUri.PostJsonToUrlAsync( new SeqLogRequest(entry), webRequest => { if (!string.IsNullOrWhiteSpace(feature.ApiKey)) { webRequest.Headers.Add("X-Seq-ApiKey", feature.ApiKey); } }); } }
private void BufferedLogEntries(SeqRequestLogEntry entry) { // TODO add buffering to logging for perf // scope to force json camel casing off using (JsConfig.With(emitCamelCaseNames: false)) { eventsUri.PostJsonToUrlAsync( new SeqLogRequest(entry), webRequest => { if(!string.IsNullOrWhiteSpace(feature.ApiKey)) webRequest.Headers.Add("X-Seq-ApiKey", feature.ApiKey); }); } }
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); }
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) 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; }