10

I would like to log the entire request & response pair for a servicestack webservice. I've looked at the response filer, however the stream exposed is read only.

2 Answers 2

12

Have you seen ServiceStack's built-in Request Logger Plugin? It's a configurable In Memory, CSV or Redis Request Logger that maintains a log / (error responses) of the recent requests.

If you would like a different behaviour, you can implement and register your own IRequestLogger, it gets called for every request. It's log method gets called for every request with the following parameters.

Log(IRequest httpReq, object requestDto, object response, TimeSpan duration);

From the IRequest you can get the original .NET Core/ASP.NET/HttpListener Request / Response types with

var originalReq = httpReq.OriginalRequest;
var originalRes = httpReq.OriginalResponse;

Filtering the response

Otherwise the ways to introspect the Response is with either

Sign up to request clarification or add additional context in comments.

17 Comments

I added Plugins.Add(new RequestLogsFeature(){ RequiredRoles = new string[]{}}); to the AppHostBase.Configure() but when I visit the /api/requestlogs it's blank, a breakpoint shows the Run method in my ServiceBase<RegisterNewMember> is being executed.
It works for me, everywhere I use it. You can see it enabled and working in the SocialBootstrapApi demo template: github.com/ServiceStack/SocialBootstrapApi
bootstrap is blank too, but I noticed the json version has data, but the duration is not numeric... Error: Parse error on line 1: ...},"requestDuration":PT0.8884481S},{"id": Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['
Also ... Which method would be best if I want to inspect/log the raw xml from both request & response? P.S. sorry: I didn't intend to use up so much of your time :-}
@JaredBeach You're welcome to include your own answer here.
|
1

This RequestFilter allows you to print raw request content. You could modify it to get more information like headers and cookies if desired. As @mythz notes in a comment, this is less detailed than the Request Logger plugin. But if you only want to add logging for a single request, it could be preferable. Disclaimer: this code is probably not production-ready and I'm not using it for production.

public class RequestDataSpyAttribute : RequestFilterAttribute
{
    // gets injected by ServiceStack
    public ILog Log { get; set; }

    private readonly string _logMessage;

    public RequestDataSpyAttribute(string logMessage)
    {
        _logMessage = logMessage;
    }

    public override void Execute(IRequest req, IResponse res, object requestDto)
    {
        System.Web.HttpRequestWrapper original = req.OriginalRequest as System.Web.HttpRequestWrapper;
        if (original == null)
            return;

        Log.Debug($"{_logMessage} Request: {original.InputStream.ReadToEnd()}");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.