how do I log requests and responses for debugging in servicestack

僤鯓⒐⒋嵵緔 提交于 2020-01-02 01:59:11

问题


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.


回答1:


Have you seen ServiceStack's built-in Request Logger Plugin? It's a configurable in-memory 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.

void Log(IRequestContext requestContext, object requestDto, 
         object response, TimeSpan elapsed);

You can also retrieve the IHttpRequest / IHttpRespnse pair from the Request Context with:

var httpReq = requestContext.Get<IHttpRequest>();
var httpRes = requestContext.Get<IHttpResponse>();

From here you can get the original 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

  • The Response Filter or Response Filter Attributes; or
  • Overriding OnAfterExecute() on your own base class that inherits from Rest/ServiceBase


来源:https://stackoverflow.com/questions/11605935/how-do-i-log-requests-and-responses-for-debugging-in-servicestack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!