Access IRequestContext in ServiceStack Plugin

I am trying to create a plugin based on the ServiceStack IPlugin interface, which can measure the elapsed time of operations and publish it on the information panel. The code itself would be quite simple, and I tried to do this based on some concepts in Request Logger .

This logger uses StopWatch , which is added inside the ServiceRunner class by default, but it only works when you configure the request logger.

I already have a custom ServiceRunner, and StopWatch is initialized here, but using this approach is not optimal, since the plugin is not standalone.

My biggest problem right now is that I apparently cannot access the IRequestContext. Is there a way a plugin can access this context, or in any other way to measure the time it takes to run requests inside a simple plugin that is not dependent on ServiceRunner?

Thanks!

+8
c # servicestack
source share
1 answer

Save the start time of the request in the RequestFilter, and then in the ResponseFilter calculate the time using the current time minus the start time?

This can be done in the plugin.

Here is some pseudo code ...

appHost.RequestFilters.Add( ( req, res, obj ) => { if(!req.Items.ContainsKey( "begin-time" ) ) req.Items.Add( "begin-time", DateTime.Now ); else req.Items[ "begin-time" ] = DateTime.Now; } ); appHost.ResponseFilters.Add( ( req, res, i ) => { var beginTime = (DateTime)req.Items[ "begin-time" ]; var elapsed = DateTime.Now - beginTime; } ); 
+1
source share

All Articles