Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • jake williamson 207 posts 872 karma points
    Sep 08, 2018 @ 16:25
    jake williamson
    0

    can i display MiniProfiler all the time for localhost?

    hey out there,

    i've got MiniProfiler displaying on my site by adding 'umbDebug=true' to the querystring.

    however... ideally i'd like MiniProfiler to always show while i'm developing locally.

    is there a simple way to do this? having to add 'umbDebug=true' to the querystring all the time is a bit of a pain as i want to keep an eye on the profile output all the time...

    it feels like an appsetting in the webconfig or something in the global.asax?

    found quite a few suggestions floating around in the forum but they don't appear to work with v7.12.2...

    any suggestions would be grand ;)

    cheers,

    jake

  • Marc Goodson 2138 posts 14321 karma points MVP 8x c-trib
    Sep 09, 2018 @ 01:23
    Marc Goodson
    0

    Hi Jake

    looking at the source, whether to profile or not seems based on this logic:

     private bool ShouldProfile(object sender)
            {
                if (GlobalSettings.DebugMode == false)
                    return false;
    
                //will not run in medium trust
                if (SystemUtilities.GetCurrentTrustLevel() < AspNetHostingPermissionLevel.High)
                    return false;
    
                var request = TryGetRequest(sender);
    
                if (request.Success == false || request.Result.Url.IsClientSideRequest())
                    return false;
    
                //if there is an umbDebug query string than profile it
                bool umbDebug;
                if (string.IsNullOrEmpty(request.Result.QueryString["umbDebug"]) == false && bool.TryParse(request.Result.QueryString["umbDebug"], out umbDebug))
                    return true;
    
                //if there is an umbDebug header than profile it
                if (string.IsNullOrEmpty(request.Result.Headers["X-UMB-DEBUG"]) == false && bool.TryParse(request.Result.Headers["X-UMB-DEBUG"], out umbDebug))
                    return true;
    
                //everything else is ok to profile
                return false;
            }
    

    therefore I think other than the QueryString if you can add either via IIS or programmatically add the Header 'X-UMB-DEBUG' to the headers collection on all responses... that might work...

    regards

    Marc

  • jake williamson 207 posts 872 karma points
    Sep 09, 2018 @ 11:50
    jake williamson
    0

    hey ya mark,

    thank you for the reply - that looks like it should do the do!

    so, this is what i tried. first up, i download the source and complied the dlls so i could debug the code and see what's happening.

    then i tried adding the header in the web.config:

    <system.webServer>
      <httpProtocol>
        <customHeaders>
          <add name="X-UMB-DEBUG" value="true" />
        </customHeaders>
      </httpProtocol>
    </system.webServer>
    

    stepping through the code, the header doesn't appear to be there...

    so then i tried setting it in the global.asax:

    public class Global : Umbraco.Web.UmbracoApplication
    {
        private void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpContext.Current.Response.AddHeader("X-UMB-DEBUG", "true");
        }
    }
    

    when i debug, the line gets hit but the header still doesn't appear to be there...

    so then on the advice on another post i found i added a module:

    public class UmbDebugHeaderModule : IHttpModule
    {
        void IHttpModule.Init(HttpApplication context)
        {
            context.BeginRequest += ContextBeginRequest;
        }
    
        private void ContextBeginRequest(object sender, EventArgs e)
        {
            var app = sender as HttpApplication;
    
            app?.Request.Headers.Add("X-UMB-DEBUG", "true");
        }
    
        void IHttpModule.Dispose() { }
    }
    

    this time, the header gets added and i get to the right line in the 'ShouldProfile' and it returns true:

    //if there is an umbDebug header than profile it
    if (string.IsNullOrEmpty(request.Result.Headers["X-UMB-DEBUG"]) == false && bool.TryParse(request.Result.Headers["X-UMB-DEBUG"], out umbDebug))
    {
        return true;
    }
    

    but no dice! the profiler doesn't show...

    i've debugged the same code with the 'umbDebug' querystring value set to true and the profiler shows as expected...

    damn. feels so close!

  • Marc Goodson 2138 posts 14321 karma points MVP 8x c-trib
    Sep 09, 2018 @ 15:21
    Marc Goodson
    0

    Oh, I'm not sure.. I'd expect if that returned true, the profiler would appear...

    thinking slightly differently, how about a rewrite rule to add the querystring to all requests eg:

       <rewrite>
          <rules>
            <rule name="addUmbDebug">
              <match url="(.*)" />
              <action  type="Rewrite" url="{R:0}?umbDebug=true" />
            </rule>
          </rules>
        </rewrite>
    
  • jake williamson 207 posts 872 karma points
    Sep 11, 2018 @ 14:46
    jake williamson
    101

    hi mark,

    i really thought the module adding the header would do it... i've spent a while on it today and although the header is defo there and the method is returning true, the damn profiler is just refusing to show.

    however...

    i gave your rewrite idea a pop and hey presto, we have a winner ;)

    i have made one modification though:

    <rule name="addUmbDebug">
        <match url="(.*)" />
        <conditions>
            <add input="{QUERY_STRING}" pattern="^(?!umbDebug=(true))" />
        </conditions>
        <action type="Rewrite" url="{R:0}?umbDebug=true" />
    </rule>
    

    i noticed in the pager i have on the site i'm deving was building up a url carrying over the existing querystring params meaning the 'umbDebug' param was being duplicated for each click (meaning the profiler disappeared). based on my experiments, the condition means the param is only added if the url doesn't already have it.

    one interesting knock on of using the rewrite is that the back office requests that take place in the background are also being profiled along with the page view request! so far i'm seeing results for:

    /umbraco/backoffice/UmbracoApi/Authentication/GetRemainingTimeoutSeconds?umbDebug=true
    /umbraco/ping.aspx
    /Application?umbDebug=true
    

    in addition, i couldn't load the backoffice... all urls resulted in a 404...

    so i had a bit more of a play with the rewrite and added some more conditions:

    this appears to be a bit happier ;)

    the only pain is that the conditions now mean my api controllers that inherit from 'UmbracoApiController' no longer profile as the urls have '/umbraco' in them e.g.

    /Umbraco/Api/Search/Get/
    

    feels pretty close though! i'd love the add header module to work but until then, i think i'm gonna roll with this.

    top suggestion!

Please Sign in or register to post replies

Write your reply to:

Draft