Skip to content

Commit 50be2c6

Browse files
committed
Allow InMemoryRollingRequestLogger to handle non-http request contexts
1 parent 3195dab commit 50be2c6

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

src/ServiceStack.ServiceInterface/Providers/InMemoryRollingRequestLogger.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,30 +44,34 @@ public void Log(IRequestContext requestContext, object requestDto, object respon
4444
&& ExcludeRequestDtoTypes.Contains(requestType))
4545
return;
4646

47-
var httpReq = requestContext.Get<IHttpRequest>();
4847
var entry = new RequestLogEntry {
4948
Id = Interlocked.Increment(ref requestId),
5049
DateTime = DateTime.UtcNow,
51-
HttpMethod = httpReq.HttpMethod,
52-
AbsoluteUri = httpReq.AbsoluteUri,
53-
PathInfo = httpReq.PathInfo,
54-
IpAddress = requestContext.IpAddress,
55-
ForwardedFor = httpReq.Headers[HttpHeaders.XForwardedFor],
56-
Referer = httpReq.Headers[HttpHeaders.Referer],
57-
Headers = httpReq.Headers.ToDictionary(),
58-
UserAuthId = httpReq.GetItemOrCookie(HttpHeaders.XUserAuthId),
59-
SessionId = httpReq.GetSessionId(),
60-
Items = httpReq.Items,
61-
Session = EnableSessionTracking ? httpReq.GetSession() : null,
6250
RequestDuration = requestDuration,
6351
};
6452

53+
var httpReq = requestContext != null ? requestContext.Get<IHttpRequest>() : null;
54+
if (httpReq != null)
55+
{
56+
entry.HttpMethod = httpReq.HttpMethod;
57+
entry.AbsoluteUri = httpReq.AbsoluteUri;
58+
entry.PathInfo = httpReq.PathInfo;
59+
entry.IpAddress = requestContext.IpAddress;
60+
entry.ForwardedFor = httpReq.Headers[HttpHeaders.XForwardedFor];
61+
entry.Referer = httpReq.Headers[HttpHeaders.Referer];
62+
entry.Headers = httpReq.Headers.ToDictionary();
63+
entry.UserAuthId = httpReq.GetItemOrCookie(HttpHeaders.XUserAuthId);
64+
entry.SessionId = httpReq.GetSessionId();
65+
entry.Items = httpReq.Items;
66+
entry.Session = EnableSessionTracking ? httpReq.GetSession() : null;
67+
}
68+
6569
if (HideRequestBodyForRequestDtoTypes != null
6670
&& requestType != null
6771
&& !HideRequestBodyForRequestDtoTypes.Contains(requestType))
6872
{
6973
entry.RequestDto = requestDto;
70-
entry.FormData = httpReq.FormData.ToDictionary();
74+
if (httpReq != null) entry.FormData = httpReq.FormData.ToDictionary();
7175
}
7276
if (response.IsErrorResponse()) {
7377
if (EnableResponseTracking)

src/ServiceStack.ServiceInterface/ServiceBase.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
22
using System.Diagnostics;
3-
using System.Web;
43
using ServiceStack.CacheAccess;
5-
using ServiceStack.CacheAccess.Providers;
64
using ServiceStack.Common;
75
using ServiceStack.Common.Web;
86
using ServiceStack.Logging;
@@ -12,7 +10,6 @@
1210
using ServiceStack.ServiceInterface.ServiceModel;
1311
using ServiceStack.Text;
1412
using ServiceStack.WebHost.Endpoints;
15-
using ServiceStack.WebHost.Endpoints.Extensions;
1613

1714
namespace ServiceStack.ServiceInterface
1815
{

0 commit comments

Comments
 (0)