scoob
scoob

Reputation: 1379

ServiceStack RequestLogger only logs one service call

Lets say I have a Service that calls a bunch of other services through the Gateway.Send :

public class SomeService : Service {  

       public SomeServiceResponse Any (SomeServiceRequest request) {

           var response1 = Gateway.Send<AnotherServiceResponse> (new AnotherServiceRequest());

           var response2 = Gateway.Send<YetAnotherServiceResponse> (new YetAnotherServiceRequest());

           var response3 = Gateway.Send<LastServiceResponse> (new LastServiceResponse());

       }
}

In this case, the custom IRequestLogger that I registered at startup will only have its Log() method called once when the first Gateway.Send() is called.

After looking at service stack's code and inspecting the Request (ASP.NET request) object - it seems after the first log, an _logged flag is added and this prevents logging on the other Gateway calls.

What would be the prefered solution to have Service Stack really log all these calls?

I temporarily added this in my RequestLogger's Log method :

public void Log(IRequest request, object requestDto, object response, TimeSpan elapsed)
{ 
(...)

// service stack sets a flag on parent request that logged once.. clear it to support multiple service calls
            if (request.Items.ContainsKey(Keywords.HasLogged)){
                request.Items.Remove(Keywords.HasLogged);
            }
}

However, this feels somewhat hacky... e.g. why is this flag even there in the first place?

(I'm using an old version of SS (5.0.2) but I notice the ServiceRunner code that sets the flag is also there in latest version)

Upvotes: 2

Views: 95

Answers (1)

mythz
mythz

Reputation: 143339

The flag for the Request Logger is precisely so it only logs actual HTTP Service requests, not "in process" requests, i.e. it prevents the behavior you want that would result in confusing and false reporting since it would repeat the HTTP headers of original request not of the Gateway request which doesn't have any.

You could add any custom logic for Gateway requests in the IAppHost.GatewayResponseFilters custom hook which only gets fired for Service Gateway requests, e.g:

GatewayResponseFilters.Add((req,response) => {
    req.TryResolve<IRequestLogger>.Log(req, req.Dto, response, TimeSpan.Zero);
});

Upvotes: 3

Related Questions