Skip to content

Commit ac5539a

Browse files
nzdevJasonElkin
authored andcommitted
Avoid string.Format allocations when profiler is not enabled.
1 parent d8b0616 commit ac5539a

File tree

6 files changed

+19
-2
lines changed

6 files changed

+19
-2
lines changed

src/Umbraco.Core/Logging/IProfiler.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,9 @@ public interface IProfiler
2727
/// authenticated or you want to clear the results, based upon some other mechanism.
2828
/// </remarks>
2929
void Stop(bool discardResults = false);
30+
31+
/// <summary>
32+
/// Whether the profiler is enabled.
33+
/// </summary>
34+
bool IsEnabled => true;
3035
}

src/Umbraco.Core/Logging/LogProfiler.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public void Stop(bool discardResults = false)
3535
// the log never stops
3636
}
3737

38+
/// <inheritdoc />
39+
public bool IsEnabled => _logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug);
40+
3841
// a lightweight disposable timer
3942
private class LightDisposableTimer : DisposableObjectSlim
4043
{

src/Umbraco.Core/Logging/NoopProfiler.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ public void Stop(bool discardResults = false)
1414
{
1515
}
1616

17+
/// <inheritdoc/>
18+
public bool IsEnabled => false;
19+
1720
private class VoidDisposable : DisposableObjectSlim
1821
{
1922
protected override void DisposeResources()

src/Umbraco.Web.Common/Profiler/WebProfiler.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public class WebProfiler : IProfiler
2424
private int _first;
2525
private MiniProfiler? _startupProfiler;
2626

27+
/// <inheritdoc />
28+
public bool IsEnabled => true;
29+
2730
public IDisposable? Step(string name) => MiniProfiler.Current?.Step(name);
2831

2932
public void Start()

src/Umbraco.Web.Website/ViewEngines/ProfilingViewEngine.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ public ProfilingViewEngine(IViewEngine inner, IProfiler profiler)
1919

2020
public ViewEngineResult FindView(ActionContext context, string viewName, bool isMainPage)
2121
{
22-
using (_profiler.Step(string.Format("{0}.FindView, {1}, {2}", _name, viewName, isMainPage)))
22+
using (_profiler.IsEnabled ? _profiler.Step(string.Format("{0}.FindView, {1}, {2}", _name, viewName, isMainPage)) : null)
2323
{
2424
return WrapResult(Inner.FindView(context, viewName, isMainPage));
2525
}
2626
}
2727

2828
public ViewEngineResult GetView(string? executingFilePath, string viewPath, bool isMainPage)
2929
{
30-
using (_profiler.Step(string.Format("{0}.GetView, {1}, {2}, {3}", _name, executingFilePath, viewPath, isMainPage)))
30+
using (_profiler.IsEnabled ? _profiler.Step(string.Format("{0}.GetView, {1}, {2}, {3}", _name, executingFilePath, viewPath, isMainPage)) : null)
3131
{
3232
return Inner.GetView(executingFilePath, viewPath, isMainPage);
3333
}

tests/Umbraco.Tests.Common/TestHelpers/Stubs/TestProfiler.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public void Stop(bool discardResults = false)
3939
}
4040
}
4141

42+
/// <inheritdoc/>
43+
public bool IsEnabled => _enabled;
44+
4245
public static void Enable() => _enabled = true;
4346

4447
public static void Disable() => _enabled = false;

0 commit comments

Comments
 (0)