forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInMemoryRollingRequestLogger.cs
More file actions
120 lines (99 loc) · 4.31 KB
/
Copy pathInMemoryRollingRequestLogger.cs
File metadata and controls
120 lines (99 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using ServiceStack.Common.Net30;
using ServiceStack.Common.Web;
using ServiceStack.ServiceHost;
using ServiceStack.ServiceInterface.ServiceModel;
using ServiceStack.ServiceModel;
namespace ServiceStack.ServiceInterface.Providers
{
public class InMemoryRollingRequestLogger : IRequestLogger
{
private static int requestId = 0;
public const int DefaultCapacity = 1000;
private readonly ConcurrentQueue<RequestLogEntry> logEntries = new ConcurrentQueue<RequestLogEntry>();
readonly int capacity;
public bool EnableSessionTracking { get; set; }
public bool EnableRequestBodyTracking { get; set; }
public bool EnableResponseTracking { get; set; }
public bool EnableErrorTracking { get; set; }
public string[] RequiredRoles { get; set; }
public Type[] ExcludeRequestDtoTypes { get; set; }
public Type[] HideRequestBodyForRequestDtoTypes { get; set; }
public InMemoryRollingRequestLogger(int? capacity = DefaultCapacity)
{
this.capacity = capacity.GetValueOrDefault(DefaultCapacity);
}
public void Log(IRequestContext requestContext, object requestDto, object response, TimeSpan requestDuration)
{
var requestType = requestDto != null ? requestDto.GetType() : null;
if (ExcludeRequestDtoTypes != null
&& requestType != null
&& ExcludeRequestDtoTypes.Contains(requestType))
return;
var entry = new RequestLogEntry {
Id = Interlocked.Increment(ref requestId),
DateTime = DateTime.UtcNow,
RequestDuration = requestDuration,
};
var httpReq = requestContext != null ? requestContext.Get<IHttpRequest>() : null;
if (httpReq != null)
{
entry.HttpMethod = httpReq.HttpMethod;
entry.AbsoluteUri = httpReq.AbsoluteUri;
entry.PathInfo = httpReq.PathInfo;
entry.IpAddress = requestContext.IpAddress;
entry.ForwardedFor = httpReq.Headers[HttpHeaders.XForwardedFor];
entry.Referer = httpReq.Headers[HttpHeaders.Referer];
entry.Headers = httpReq.Headers.ToDictionary();
entry.UserAuthId = httpReq.GetItemOrCookie(HttpHeaders.XUserAuthId);
entry.SessionId = httpReq.GetSessionId();
entry.Items = httpReq.Items;
entry.Session = EnableSessionTracking ? httpReq.GetSession() : null;
}
if (HideRequestBodyForRequestDtoTypes != null
&& requestType != null
&& !HideRequestBodyForRequestDtoTypes.Contains(requestType))
{
entry.RequestDto = requestDto;
if (httpReq != null)
{
entry.FormData = httpReq.FormData.ToDictionary();
if (EnableRequestBodyTracking)
{
entry.RequestBody = httpReq.GetRawBody();
}
}
}
if (!response.IsErrorResponse()) {
if (EnableResponseTracking)
entry.ResponseDto = response;
}
else {
if (EnableErrorTracking)
entry.ErrorResponse = ToSerializableErrorResponse(response);
}
logEntries.Enqueue(entry);
RequestLogEntry dummy;
if (logEntries.Count > capacity)
logEntries.TryDequeue(out dummy);
}
public List<RequestLogEntry> GetLatestLogs(int? take)
{
var requestLogEntries = logEntries.ToArray();
return take.HasValue
? new List<RequestLogEntry>(requestLogEntries.Take(take.Value))
: new List<RequestLogEntry>(requestLogEntries);
}
public static object ToSerializableErrorResponse(object response)
{
var errorResult = response as IHttpResult;
if (errorResult != null)
return errorResult.Response;
var ex = response as Exception;
return ex != null ? ex.ToResponseStatus() : null;
}
}
}