Question

I would like to use the MiniProfiler for my MVC3 application, so I followed Scott Hanselman's blog post

My Global.asax.cs file has the necessary changes like in the source's MVC sample.

But I would like to measure a particular call in my controller. So I put this code in controller:

if (Request.IsLocal)
{
    var profiler = MiniProfiler.Current;
    using (profiler.Step("SelectUserDetail Function"))
    {
        user = UserService.SelectUserDetail(userId);
    }
}

I suspect my code will never in production environment as I'm wrapping this block in a Request.IsLocal check.

How can I do this check for only for local call or if I run in debug mode? At any case, it should execute the user = UserService.SelectUserDetail(userId) statement.

Was it helpful?

Solution

If I understand your question correctly, you're only wanting to call MiniProfiler's .Step() extension method when running locally (or debugging), correct?

If so, this kinda defeats the purpose of MiniProfiler, which is to have all this instrumentation available for production code, without impacting production.

I'm confident you can simply do this in your code:

using (MiniProfiler.Current.Step("SelectUserDetail Function"))
{
    user = UserService.SelectUserDetail(userId);
}

and it will have virtually no impact on your app; we literally do this hundreds of times in our code here on Stack Overflow without issue (as well as every single database query).

You should only need to have your checks when a new request comes in:

protected void Application_BeginRequest()
{
    if (Request.IsLocal) { MiniProfiler.Start(); }
}

When you're running in production, any calls to MiniProfiler.Current.Step() will return nothing, since the profiler is null (the beauty of extension methods).

If you still want to prevent any using statements from appearing in your production code, you should familiarize yourself with preprocessor directives. See this question, as well. However, I would strongly advise against them for this purpose, as it isn't necessary.

OTHER TIPS

I usually create something like DebugHelper static class and define there:

public static class DebugHelper
        {
            private static bool? _isDebugEnabled = false;
            public static bool IsDebug
            {
                get
                {
                    if (!_isDebugEnabled.HasValue)
                    {
                        _isDebugEnabled = false;
    #if DEBUG
                        _isDebugEnabled = true;
    #endif
                    }
                    //may be extra rules like   check for some debug key in HttpContext.Current etc.
                    return _isDebugEnabled.Value;
                }
                set { _isDebugEnabled = value; }
            }

            public static bool IsDevEnvironment
            {
                get
                {
                    string environment = settingsService.GetSettingByKey<string>("environment");
                    return environment == "dev";
                }
            }
            public static bool IsTestEnvironment
            {
                get
                {
                    string environment = settingsService.GetSettingByKey<string>("environment");
                    return environment == "test";
                }
            }

DebuHelper allows me easily switch on/switch off debug mode, logging, tracing etc. add extra output or whatever for dev and test environment

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top