1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00
Sonarr/NzbDrone.Web/App_Start/MiniProfiler.cs

113 lines
4.1 KiB
C#
Raw Normal View History

2012-07-28 00:31:33 +03:00
using System;
using System.Web;
using System.Web.Mvc;
using System.Linq;
2012-07-28 00:31:33 +03:00
using StackExchange.Profiling;
using StackExchange.Profiling.MVCHelpers;
using Microsoft.Web.Infrastructure;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
//using System.Data;
//using System.Data.Entity;
//using System.Data.Entity.Infrastructure;
2012-07-28 00:31:33 +03:00
//using StackExchange.Profiling.Data.EntityFramework;
//using StackExchange.Profiling.Data.Linq2Sql;
2012-07-28 00:31:33 +03:00
[assembly: WebActivator.PreApplicationStartMethod(
typeof(NzbDrone.Web.App_Start.MiniProfilerPackage), "PreStart")]
2012-07-28 00:31:33 +03:00
[assembly: WebActivator.PostApplicationStartMethod(
typeof(NzbDrone.Web.App_Start.MiniProfilerPackage), "PostStart")]
2012-07-28 00:31:33 +03:00
namespace NzbDrone.Web.App_Start
{
public static class MiniProfilerPackage
{
public static void PreStart()
{
// Be sure to restart you ASP.NET Developement server, this code will not run until you do that.
//TODO: See - _MINIPROFILER UPDATED Layout.cshtml
2012-07-28 00:31:33 +03:00
// For profiling to display in the UI you will have to include the line @StackExchange.Profiling.MiniProfiler.RenderIncludes()
// in your master layout
2012-07-28 00:31:33 +03:00
//TODO: Non SQL Server based installs can use other formatters like: new StackExchange.Profiling.SqlFormatters.InlineFormatter()
MiniProfiler.Settings.SqlFormatter = new StackExchange.Profiling.SqlFormatters.SqlServerFormatter();
2012-07-28 00:31:33 +03:00
//TODO: To profile a standard DbConnection:
// var profiled = new ProfiledDbConnection(cnn, MiniProfiler.Current);
//TODO: If you are profiling EF code first try:
2012-07-28 00:31:33 +03:00
// MiniProfilerEF.Initialize();
//Make sure the MiniProfiler handles BeginRequest and EndRequest
DynamicModuleUtility.RegisterModule(typeof(MiniProfilerStartupModule));
//Setup profiler for Controllers via a Global ActionFilter
GlobalFilters.Filters.Add(new ProfilingActionFilter());
2012-07-28 00:31:33 +03:00
// You can use this to check if a request is allowed to view results
//MiniProfiler.Settings.Results_Authorize = (request) =>
//{
// you should implement this if you need to restrict visibility of profiling on a per request basis
// return !DisableProfilingResults;
//};
// the list of all sessions in the store is restricted by default, you must return true to alllow it
//MiniProfiler.Settings.Results_List_Authorize = (request) =>
//{
// you may implement this if you need to restrict visibility of profiling lists on a per request basis
//return true; // all requests are kosher
//};
}
public static void PostStart()
{
// Intercept ViewEngines to profile all partial views and regular views.
// If you prefer to insert your profiling blocks manually you can comment this out
var copy = ViewEngines.Engines.ToList();
ViewEngines.Engines.Clear();
foreach (var item in copy)
{
ViewEngines.Engines.Add(new ProfilingViewEngine(item));
}
}
}
public class MiniProfilerStartupModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += (sender, e) =>
{
2012-07-28 00:31:33 +03:00
var request = ((HttpApplication)sender).Request;
//TODO: By default only local requests are profiled, optionally you can set it up
// so authenticated users are always profiled
2012-07-28 01:33:47 +03:00
//if (request.IsLocal) { MiniProfiler.Start(); }
MiniProfiler.Start();
};
2012-07-28 00:31:33 +03:00
// TODO: You can control who sees the profiling information
/*
context.AuthenticateRequest += (sender, e) =>
{
if (!CurrentUserIsAllowedToSeeProfiler())
{
2012-07-28 00:31:33 +03:00
StackExchange.Profiling.MiniProfiler.Stop(discardResults: true);
}
};
*/
2012-07-28 00:31:33 +03:00
context.EndRequest += (sender, e) =>
{
MiniProfiler.Stop();
};
}
public void Dispose() { }
}
}