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.

Myster
  • 17,704
  • 13
  • 64
  • 93

2 Answers2

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

mythz
  • 141,670
  • 29
  • 246
  • 390
  • 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 is being executed. – Myster Jul 24 '12 at 05:33
  • It works for me, everywhere I use it. You can see it enabled and working in the SocialBootstrapApi demo template: https://github.com/ServiceStack/SocialBootstrapApi – mythz Jul 24 '12 at 06:43
  • 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', '{', '[' – Myster Jul 24 '12 at 22:08
  • 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 :-} – Myster Jul 24 '12 at 22:17
  • Use the Network tab in Chrome WebInspector or Fiddler for HTTP traffic. Whilst WireShark shows you everything. – mythz Jul 24 '12 at 22:31
  • ok, This is server-server communication so I'll have to grapple with wireshark :-) – Myster Jul 25 '12 at 21:33
  • oh, would something like this http://stackoverflow.com/questions/11084459/logging-raw-and-compressed-http-responses-in-asp-net-iis7 work if I wanted to log the raw data? – Myster Jul 26 '12 at 07:10
  • @mythz where can I register the `IRequestLogger`? There's no doco on how to register it, I assume it just automatically finds it? – Phill May 23 '13 at 02:52
  • @mythz we want to log requests to a txt file, no route or anything like that. So we don't need the RequestLogsFeature. Just want to capture/log. – Phill May 23 '13 at 03:00
  • NEVER mind. I registered it wrong, its being picked up fine. :) Sorry. – Phill May 23 '13 at 03:18
  • Has anyone created one a RequestLogger to write to Azure table storage? Pretty simple to modify the sample stuff in ServiceStack but no point reinventing the wheel.... – Rory Jun 21 '13 at 15:35
  • @mythz, is there a way to do this for a particular request and not all requests? – Jared Beach Mar 09 '21 at 18:07
  • @JaredBeach You can specify which requests to skip with the `SkipLogging` predicate returning true on all requests but the ones you want logged. Whilst the `ExcludeRequestDtoTypes` collection lets you specify Request DTOs you want excluded. – mythz Mar 09 '21 at 18:20
  • @mythz figured out how to do it with an attribute https://gist.github.com/jmbeach/7f0a8a9371e72e8f5f73e3b6470fb6a4 – Jared Beach Mar 09 '21 at 18:40
  • @JaredBeach that's just doing simple text logging, it's not using the much more detailed [request logger](https://docs.servicestack.net/request-logger). – mythz Mar 09 '21 at 18:49
  • Request attribute version works perfectly for my use case. Just a temporary solution to see if a third party is integrating with our app correctly. May be worth adding this approach to your answer. – Jared Beach Mar 09 '21 at 18:52
  • 1
    @JaredBeach You're welcome to include your own answer here. – mythz Mar 09 '21 at 18:59
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()}");
    }
}
Jared Beach
  • 2,635
  • 34
  • 38