This commit is contained in:
Bjarke Berg
2021-01-13 08:30:35 +01:00
parent cbe2214afc
commit 8c8871a799
3 changed files with 20 additions and 15 deletions

View File

@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
// Copyright (c) Umbraco.
// See LICENSE for more details.
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Umbraco.Core.Events;
using Umbraco.Core.Logging;
@@ -10,26 +10,38 @@ using Umbraco.Web.Common.Lifetime;
namespace Umbraco.Web.Common.Profiler
{
/// <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;
private readonly List<Action> _terminate = new List<Action>();
/// <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;
// although registered in WebRuntime.Compose, ensure that we have not
// 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) return;
if (_profiler != null)
{
return;
}
// if VoidProfiler was registered, let it be known
if (profiler is NoopProfiler)
{
logger.LogInformation(
"Profiler is VoidProfiler, not profiling (must run debug mode to profile).");
}
_profile = false;
}
@@ -38,13 +50,9 @@ namespace Umbraco.Web.Common.Profiler
{
if (_profile)
{
void requestStart(object sender, HttpContext context) => _profiler.UmbracoApplicationBeginRequest(context);
_umbracoRequestLifetime.RequestStart += requestStart;
_terminate.Add(() => _umbracoRequestLifetime.RequestStart -= requestStart);
_umbracoRequestLifetime.RequestStart += (sender, context) => _profiler.UmbracoApplicationBeginRequest(context);
void requestEnd(object sender, HttpContext context) => _profiler.UmbracoApplicationEndRequest(context);
_umbracoRequestLifetime.RequestEnd += requestEnd;
_terminate.Add(() => _umbracoRequestLifetime.RequestEnd -= requestEnd);
_umbracoRequestLifetime.RequestEnd += (sender, context) => _profiler.UmbracoApplicationEndRequest(context);
// Stop the profiling of the booting process
_profiler.StopBoot();