Files
Umbraco-CMS/src/Umbraco.Web.Common/Profiler/InitializeWebProfiling.cs

61 lines
2.1 KiB
C#
Raw Normal View History

2021-01-13 08:30:35 +01:00
// Copyright (c) Umbraco.
// See LICENSE for more details.
using Microsoft.Extensions.Logging;
using Umbraco.Core.Events;
using Umbraco.Core.Logging;
using Umbraco.Web.Common.Lifetime;
namespace Umbraco.Web.Common.Profiler
{
2021-01-13 08:30:35 +01:00
/// <summary>
/// Initialized the web profiling. Ensures the boot process profiling is stopped.
/// </summary>
public class InitializeWebProfiling : INotificationHandler<UmbracoApplicationStarting>
{
private readonly bool _profile;
private readonly WebProfiler _profiler;
private readonly IUmbracoRequestLifetime _umbracoRequestLifetime;
2021-01-13 08:30:35 +01:00
/// <summary>
/// Initializes a new instance of the <see cref="InitializeWebProfiling"/> class.
/// </summary>
public InitializeWebProfiling(IProfiler profiler, IUmbracoRequestLifetime umbracoRequestLifetime, ILogger<InitializeWebProfiling> logger)
{
_umbracoRequestLifetime = umbracoRequestLifetime;
_profile = true;
2021-01-13 08:30:35 +01:00
// although registered in UmbracoBuilderExtensions.AddUmbraco, ensure that we have not
// been replaced by another component, and we are still "the" profiler
_profiler = profiler as WebProfiler;
2021-01-13 08:30:35 +01:00
if (_profiler != null)
{
return;
}
// if VoidProfiler was registered, let it be known
if (profiler is NoopProfiler)
2021-01-13 08:30:35 +01:00
{
logger.LogInformation(
"Profiler is VoidProfiler, not profiling (must run debug mode to profile).");
2021-01-13 08:30:35 +01:00
}
_profile = false;
}
/// <inheritdoc/>
public void Handle(UmbracoApplicationStarting notification)
{
if (_profile)
{
2021-01-13 08:30:35 +01:00
_umbracoRequestLifetime.RequestStart += (sender, context) => _profiler.UmbracoApplicationBeginRequest(context);
2021-01-13 08:30:35 +01:00
_umbracoRequestLifetime.RequestEnd += (sender, context) => _profiler.UmbracoApplicationEndRequest(context);
// Stop the profiling of the booting process
_profiler.StopBoot();
}
}
}
}