2022-05-09 09:39:46 +02:00
|
|
|
// Copyright (c) Umbraco.
|
2021-01-13 08:30:35 +01:00
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
2021-01-12 13:58:48 +01:00
|
|
|
using Microsoft.Extensions.Logging;
|
2021-05-18 19:19:58 +02:00
|
|
|
using Umbraco.Cms.Core;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core.Events;
|
|
|
|
|
using Umbraco.Cms.Core.Logging;
|
2021-05-11 14:33:49 +02:00
|
|
|
using Umbraco.Cms.Core.Notifications;
|
2021-01-12 13:58:48 +01:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
namespace Umbraco.Cms.Web.Common.Profiler;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initialized the web profiling. Ensures the boot process profiling is stopped.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class InitializeWebProfiling : INotificationHandler<UmbracoApplicationStartingNotification>
|
2021-01-12 13:58:48 +01:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
private readonly bool _profile;
|
|
|
|
|
private readonly WebProfiler? _profiler;
|
|
|
|
|
|
2021-01-13 08:30:35 +01:00
|
|
|
/// <summary>
|
2022-05-09 09:39:46 +02:00
|
|
|
/// Initializes a new instance of the <see cref="InitializeWebProfiling" /> class.
|
2021-01-13 08:30:35 +01:00
|
|
|
/// </summary>
|
2022-05-09 09:39:46 +02:00
|
|
|
public InitializeWebProfiling(IProfiler profiler, ILogger<InitializeWebProfiling> logger)
|
2021-01-12 13:58:48 +01:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
_profile = true;
|
2021-01-13 08:30:35 +01:00
|
|
|
|
2022-05-09 09:39:46 +02: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;
|
|
|
|
|
if (_profiler != null)
|
2021-01-12 13:58:48 +01:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
return;
|
2021-01-12 13:58:48 +01:00
|
|
|
}
|
|
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
// if VoidProfiler was registered, let it be known
|
|
|
|
|
if (profiler is NoopProfiler)
|
2021-01-12 13:58:48 +01:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
logger.LogInformation(
|
|
|
|
|
"Profiler is VoidProfiler, not profiling (must run debug mode to profile).");
|
2021-01-12 13:58:48 +01:00
|
|
|
}
|
2021-01-26 07:58:11 +01:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
_profile = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public void Handle(UmbracoApplicationStartingNotification notification)
|
|
|
|
|
{
|
|
|
|
|
if (_profile && notification.RuntimeLevel == RuntimeLevel.Run)
|
|
|
|
|
{
|
|
|
|
|
// Stop the profiling of the booting process
|
|
|
|
|
_profiler?.StopBoot();
|
|
|
|
|
}
|
2021-01-12 13:58:48 +01:00
|
|
|
}
|
|
|
|
|
}
|