I know that I can make a call to MiniProfiler.Settings.Storage.Save(MiniProfiler); to persist response times to database. But I have a bit of a more trickier problem here. We have built a little framework around the mvc mini profiler so that we could also use it without a web context and it also has regression reporting capabilities.
But cutting the long story short, here's the problem -
I've had to implement a Step method that calls the mini profiler's Step method(and does some other more stuff). This was done because we are not directly exposing the mini profiler's functionalities to our developers as is. We will be giving them different approaches like you can either use the 'mini profiler approach' or you can use the 'stopwatch approach'. So, here's the code for my Step method -
public IDisposable Step(Profiler profiler, String operationName, Int32 numofInvocations = 1, Action operation = null) //
{
if (profiler == Profiler.MVCMiniProfiler)
{
return MiniProfiler.Step(operationName); //This is statement to note in this code
}
else if (profiler == Profiler.MyStopWatch)
{
return new MyStopWatch(operationName, numofInvocations, operation);
}
else
{
return null;
}
}
Now the problem is that the Step method of MiniProfiler returns a disposable object(the Timing object in it's code) and the Dispose() of the Timing object stops the stopwatch and returns elapsed time. What I need to do is to make a call to the Save() method as soon as anything is profiled. But I also do not want to make a change to the MiniProfiler source code.
Is there any easy way of doing this?
You could make a wrapper that also implements the IDisposable
interface. Something like:
public class MiniProfilerStep : IDisposable
{
private readonly MiniProfiler _profiler;
private readonly IDisposable _step;
public MiniProfilerStep(MiniProfiler profiler, string operation)
{
_profiler = profiler;
_step = _profiler.Step(operation);
}
public void Dispose()
{
_step.Dispose();
MiniProfiler.Settings.Storage.Save(_profiler);
}
}
and then return an instance of the wrapper instead of the mini profiler step:
if (profiler == Profiler.MVCMiniProfiler)
{
return new MiniProfilerStep(MiniProfiler, operationName);
}