0.0.4 • Published 3 years ago

ng-miniprofiler v0.0.4

Weekly downloads
14
License
MIT
Repository
-
Last release
3 years ago

NgMiniProfiler

This package contains an Http interceptor for using MiniProfiler with Angular.

Getting started

To use it in your app, simply import the MiniProfilerModule and provide the interceptor.

import { MiniProfilerModule, MiniProfilerInterceptor } from 'ng-miniprofiler';

@NgModule({
  [...]
  imports: [
    MiniProfilerModule.forRoot({
      baseUri: 'http://localhost:12345',
      colorScheme: 'Auto',
      maxTraces: 15,
      position: 'BottomLeft',
      toggleShortcut: 'Alt+M',
      enabled: true,
      enableGlobalMethod: true
    }),
  ],
  providers: [
    { provide: HTTP_INTERCEPTORS, useClass: MiniProfilerInterceptor, multi: true }
  ]
})
export class YourModule { }

Configurations

Ng-miniprofiler offers a couple of configurations.

ConfigDescriptionDefault
baseUriThe base uri of the server hosting your MiniProfiler results and include.min.js file. Depending on your configs, the results may be under the profiling route. The value should then be http://localhost:12345/profiling.''
colorSchemeThe theme. Either Light, Dark or Auto.Auto
maxTracesMaximum number of traces shown.15
positionWhere the popup should be placed. Either Left, Right, BottomLeft, BottomRight.Left
toggleShortcutThe shortcut for toggling the popup.Alt+M
showControlsWhether or not the controls (minimize and clear) should be shown.false
enabledWhether or not miniprofiler is enabled.true
enableGlobalMethodWhether or not an enableMiniProfiler method should be added to the window object. Can be useful in a production environment where you want MiniProfiler to be disabed by default.true

Backend

If you are using asp.net core then you need to add two nuget packages :- MiniProfiler.AspNetCore.Mvc, MiniProfiler.EntityFrameworkCore

services.AddMiniProfiler(options =>
        {
            (options.Storage as MemoryCacheStorage).CacheDuration = TimeSpan.FromMinutes(60);

            // (Optional) Control which SQL formatter to use, InlineFormatter is the default
            options.SqlFormatter = new StackExchange.Profiling.SqlFormatters.InlineFormatter();

            // (Optional) You can disable "Connection Open()", "Connection Close()" (and async variant) tracking.
            // (defaults to true, and connection opening/closing is tracked)
            options.TrackConnectionOpenClose = true;

            // (Optional) Use something other than the "light" color scheme.
            // (defaults to "light")
            options.ColorScheme = StackExchange.Profiling.ColorScheme.Auto;

            // The below are newer options, available in .NET Core 3.0 and above:

            // (Optional) You can disable MVC filter profiling
            // (defaults to true, and filters are profiled)
            options.EnableMvcFilterProfiling = true;

            // (Optional) You can disable MVC view profiling
            // (defaults to true, and views are profiled)
            options.EnableMvcViewProfiling = true;
        }).AddEntityFramework();

You can check out the official documentation of miniprofiler(https://miniprofiler.com/dotnet/AspDotNetCore)

And if your server lives on a different domain from your Angular app, you may run into two CORS issues.

Firstly, you need to allow your app to request MiniProfiler results. For an ASP.NET Web API solution, that's one way of doing so (in the global.asax.cs file).

protected void Application_BeginRequest() 
{ 
    var context = HttpContext.Current; 
    if (HttpContext.Current.Request.Path.StartsWith("/mini-profiler-resources")) 
    { 
        var origin = context.Request.Headers.Get("Origin"); 
        if (origin != null) 
        { 
            context.Response.Headers.Add("Access-Control-Allow-Origin", origin); 
        } 
        if (context.Request.HttpMethod == "OPTIONS") 
        { 
            context.Response.StatusCode = 200; 
            context.Response.Headers.Add("Access-Control-Allow-Headers", "Content-Type"); 
            context.Response.Headers.Add("Access-Control-Allow-Methods", "OPTIONS, GET"); 
        } 
    } 
}

Secondly, your server needs to let your Angular app access the X-MiniProfiler-Ids headers. You may do that by adding an ActionFilter.

public class MiniProfilerCorsHeaderFilter : ActionFilterAttribute
{
    public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    {
        actionExecutedContext.Response.Headers.Add("Access-Control-Expose-Headers", "X-MiniProfiler-Ids");
    }
}

or by adding in CORS.

public class MiniProfilerCorsHeaderFilter : ActionFilterAttribute
{
    app.UseCors(builder =>
                builder.WithOrigins("http://example.com")
                .AllowAnyMethod()
                .AllowAnyHeader()
                .WithExposedHeaders("x-miniprofiler-ids"));
}

Enable mini-profiler manually

If you set the enableGlobalMethod configuration to true, you may call the enableMiniProfiler from your browser's devtools console to enable MiniProfiler manually.

> enableMiniProfiler();
>
> MiniProfiler loaded.