Files
Umbraco-CMS/src/Umbraco.Web/Logging/WebProfilerComponent.cs

50 lines
1.7 KiB
C#
Raw Normal View History

2016-09-08 18:43:58 +02:00
using System;
using System.Web;
using Umbraco.Core.Components;
using Umbraco.Core.Logging;
2016-09-08 18:43:58 +02:00
namespace Umbraco.Web.Logging
2016-09-08 18:43:58 +02:00
{
2019-01-04 08:36:38 +01:00
internal sealed class WebProfilerComponent : IComponent
2016-09-08 18:43:58 +02:00
{
2019-01-04 08:36:38 +01:00
private readonly WebProfiler _profiler;
2019-01-07 09:30:47 +01:00
private readonly bool _profile;
2016-09-08 18:43:58 +02:00
2019-01-07 09:30:47 +01:00
public WebProfilerComponent(IProfiler profiler, ILogger logger)
2016-09-08 18:43:58 +02:00
{
2019-01-07 09:30:47 +01:00
_profile = true;
2017-07-20 11:21:28 +02:00
// although registered in WebRuntime.Compose, ensure that we have not
// been replaced by another component, and we are still "the" profiler
2016-09-08 18:43:58 +02:00
_profiler = profiler as WebProfiler;
2019-01-07 09:30:47 +01:00
if (_profiler != null) return;
// if VoidProfiler was registered, let it be known
if (profiler is VoidProfiler)
logger.Info<WebProfilerComponent>("Profiler is VoidProfiler, not profiling (must run debug mode to profile).");
_profile = false;
}
public void Initialize()
{
if (!_profile) return;
// bind to ApplicationInit - ie execute the application initialization for *each* application
// it would be a mistake to try and bind to the current application events
UmbracoApplicationBase.ApplicationInit += InitializeApplication;
2016-09-08 18:43:58 +02:00
}
2019-01-07 09:30:47 +01:00
public void Terminate()
{ }
2016-09-08 18:43:58 +02:00
private void InitializeApplication(object sender, EventArgs args)
{
2019-01-07 09:30:47 +01:00
if (!(sender is HttpApplication app)) return;
2016-09-08 18:43:58 +02:00
// for *each* application (this will run more than once)
app.BeginRequest += (s, a) => _profiler.UmbracoApplicationBeginRequest(s, a);
app.EndRequest += (s, a) => _profiler.UmbracoApplicationEndRequest(s, a);
}
}
2017-07-20 11:21:28 +02:00
}