I have a simple web form application that uses a masterpage.
I followed the instructions on how to get mini-profiler working. I get all the stats OK. Now I do not really know how to toggle it on or off.
I thought of using a query string and look for it Application_BeginRequest
- If it is there just use the profiler the whole session.... OK- Session is not loaded at that stage and if use the Application_AcquireRequestState
and a static variable it loads it many many times and the profiler does work sometimes, sometimes not and I do not know why?
The simple way I have now is.
protected void Application_BeginRequest(object sender, EventArgs e)
{
MiniProfiler profiler = null;
if (Request.QueryString["p"] != null)
{
profiler = MiniProfiler.Start();
using (profiler.Step("Application_BeginRequest"))
{
}
}
}
So that works ok but i have to add a query param on each request. Not good. I never used global.asax
before so I am not 100% sure how it all works down there.
What is the best way I can set a variable for a pre defined time so the profiler will always load when I turn it on in a secret way?
Edit and solution to my problem
protected void Application_BeginRequest(object sender, EventArgs e)
{
MiniProfiler profiler = null;
if (Request.Cookies["profiler"] != null)
{
profiler = MiniProfiler.Start();
using (profiler.Step("Application_BeginRequest"))
{
}
}
}
If you added MiniProfiler via the MiniProfiler.MVC3 package, then it adds a C# file for your convenience in configuring it: look in App_Start\MiniProfiler.cs
, looking in particular at Init
which has the code to decide whether to start profiling (request.IsLocal
is the default)
In your case, I would suggest checking for a cookie in the start condition, and checking your more complete "am I a developer, or just someone who heard about a special cookie" in the end condition; in particular, if you call MiniProfiler.Stop(false)
, then all the data is discarded. Your "turn it on in a secret way" and "pre defined time" are then all achieved cheaply via the cookie, with the "am I a developer" there to make sure that it can't be abused just by setting a cookie.