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
109 lines (90 loc) · 4.02 KB
/
Copy pathInMemoryRollingRequestLogger.cs
File metadata and controls
109 lines (90 loc) · 4.02 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
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 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 httpReq = requestContext.Get<IHttpRequest>();
var entry = new RequestLogEntry {
Id = Interlocked.Increment(ref requestId),
DateTime = DateTime.UtcNow,
HttpMethod = httpReq.HttpMethod,
AbsoluteUri = httpReq.AbsoluteUri,
PathInfo = httpReq.PathInfo,
IpAddress = requestContext.IpAddress,
ForwardedFor = httpReq.Headers[HttpHeaders.XForwardedFor],
Referer = httpReq.Headers[HttpHeaders.Referer],
Headers = httpReq.Headers.ToDictionary(),
UserAuthId = httpReq.GetItemOrCookie(HttpHeaders.XUserAuthId),
SessionId = httpReq.GetSessionId(),
Items = httpReq.Items,
Session = EnableSessionTracking ? httpReq.GetSession() : null,
RequestDuration = requestDuration,
};
if (HideRequestBodyForRequestDtoTypes != null
&& requestType != null
&& !HideRequestBodyForRequestDtoTypes.Contains(requestType))
{
entry.RequestDto = requestDto;
entry.FormData = httpReq.FormData.ToDictionary();
}
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;
if (ex != null)
ResponseStatusTranslator.Instance.Parse(ex);
return null;
}
}
}