Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit f4a86f5

Browse files
authored
Add DiagnosticSource to RazorView (#6386)
Addresses #6222
1 parent e5da44a commit f4a86f5

File tree

8 files changed

+286
-42
lines changed

8 files changed

+286
-42
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Diagnostics;
5+
using Microsoft.AspNetCore.Mvc.Rendering;
6+
7+
namespace Microsoft.AspNetCore.Mvc.Razor.Internal
8+
{
9+
public static class MvcRazorDiagnosticSourceExtensions
10+
{
11+
public static void BeforeViewPage(
12+
this DiagnosticSource diagnosticSource,
13+
IRazorPage page,
14+
ViewContext viewContext)
15+
{
16+
if (diagnosticSource.IsEnabled("Microsoft.AspNetCore.Mvc.Razor.BeforeViewPage"))
17+
{
18+
diagnosticSource.Write(
19+
"Microsoft.AspNetCore.Mvc.Razor.BeforeViewPage",
20+
new
21+
{
22+
page = page,
23+
viewContext = viewContext,
24+
actionDescriptor = viewContext.ActionDescriptor,
25+
httpContext = viewContext.HttpContext,
26+
});
27+
}
28+
}
29+
30+
public static void AfterViewPage(
31+
this DiagnosticSource diagnosticSource,
32+
IRazorPage page,
33+
ViewContext viewContext)
34+
{
35+
if (diagnosticSource.IsEnabled("Microsoft.AspNetCore.Mvc.Razor.AfterViewPage"))
36+
{
37+
diagnosticSource.Write(
38+
"Microsoft.AspNetCore.Mvc.Razor.AfterViewPage",
39+
new
40+
{
41+
page = page,
42+
viewContext = viewContext,
43+
actionDescriptor = viewContext.ActionDescriptor,
44+
httpContext = viewContext.HttpContext,
45+
});
46+
}
47+
}
48+
}
49+
}

src/Microsoft.AspNetCore.Mvc.Razor/RazorView.cs

+23-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Microsoft.AspNetCore.Mvc.Rendering;
1111
using Microsoft.AspNetCore.Mvc.ViewEngines;
1212
using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal;
13+
using Microsoft.AspNetCore.Mvc.Razor.Internal;
1314
using Microsoft.Extensions.DependencyInjection;
1415

1516
namespace Microsoft.AspNetCore.Mvc.Razor
@@ -23,6 +24,7 @@ public class RazorView : IView
2324
private readonly IRazorViewEngine _viewEngine;
2425
private readonly IRazorPageActivator _pageActivator;
2526
private readonly HtmlEncoder _htmlEncoder;
27+
private readonly DiagnosticSource _diagnosticSource;
2628
private IViewBufferScope _bufferScope;
2729

2830
/// <summary>
@@ -34,12 +36,14 @@ public class RazorView : IView
3436
/// </param>
3537
/// <param name="razorPage">The <see cref="IRazorPage"/> instance to execute.</param>
3638
/// <param name="htmlEncoder">The HTML encoder.</param>
39+
/// <param name="diagnosticSource">The <see cref="DiagnosticSource"/>.</param>
3740
public RazorView(
3841
IRazorViewEngine viewEngine,
3942
IRazorPageActivator pageActivator,
4043
IReadOnlyList<IRazorPage> viewStartPages,
4144
IRazorPage razorPage,
42-
HtmlEncoder htmlEncoder)
45+
HtmlEncoder htmlEncoder,
46+
DiagnosticSource diagnosticSource)
4347
{
4448
if (viewEngine == null)
4549
{
@@ -66,11 +70,17 @@ public RazorView(
6670
throw new ArgumentNullException(nameof(htmlEncoder));
6771
}
6872

73+
if (diagnosticSource == null)
74+
{
75+
throw new ArgumentNullException(nameof(diagnosticSource));
76+
}
77+
6978
_viewEngine = viewEngine;
7079
_pageActivator = pageActivator;
7180
ViewStartPages = viewStartPages;
7281
RazorPage = razorPage;
7382
_htmlEncoder = htmlEncoder;
83+
_diagnosticSource = diagnosticSource;
7484
}
7585

7686
/// <inheritdoc />
@@ -152,11 +162,21 @@ private async Task<ViewBufferTextWriter> RenderPageAsync(
152162
}
153163
}
154164

155-
private Task RenderPageCoreAsync(IRazorPage page, ViewContext context)
165+
private async Task RenderPageCoreAsync(IRazorPage page, ViewContext context)
156166
{
157167
page.ViewContext = context;
158168
_pageActivator.Activate(page, context);
159-
return page.ExecuteAsync();
169+
170+
_diagnosticSource.BeforeViewPage(page, context);
171+
172+
try
173+
{
174+
await page.ExecuteAsync();
175+
}
176+
finally
177+
{
178+
_diagnosticSource.AfterViewPage(page, context);
179+
}
160180
}
161181

162182
private async Task RenderViewStartsAsync(ViewContext context)

src/Microsoft.AspNetCore.Mvc.Razor/RazorViewEngine.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class RazorViewEngine : IRazorViewEngine
4444
private readonly ILogger _logger;
4545
private readonly RazorViewEngineOptions _options;
4646
private readonly RazorProject _razorProject;
47+
private readonly DiagnosticSource _diagnosticSource;
4748

4849
/// <summary>
4950
/// Initializes a new instance of the <see cref="RazorViewEngine" />.
@@ -54,7 +55,8 @@ public RazorViewEngine(
5455
HtmlEncoder htmlEncoder,
5556
IOptions<RazorViewEngineOptions> optionsAccessor,
5657
RazorProject razorProject,
57-
ILoggerFactory loggerFactory)
58+
ILoggerFactory loggerFactory,
59+
DiagnosticSource diagnosticSource)
5860
{
5961
_options = optionsAccessor.Value;
6062

@@ -77,6 +79,7 @@ public RazorViewEngine(
7779
_htmlEncoder = htmlEncoder;
7880
_logger = loggerFactory.CreateLogger<RazorViewEngine>();
7981
_razorProject = razorProject;
82+
_diagnosticSource = diagnosticSource;
8083
ViewLookupCache = new MemoryCache(new MemoryCacheOptions());
8184
}
8285

@@ -466,7 +469,7 @@ private ViewEngineResult CreateViewEngineResult(ViewLocationCacheResult result,
466469
viewStarts[i] = viewStartItem.PageFactory();
467470
}
468471

469-
var view = new RazorView(this, _pageActivator, viewStarts, page, _htmlEncoder);
472+
var view = new RazorView(this, _pageActivator, viewStarts, page, _htmlEncoder, _diagnosticSource);
470473
return ViewEngineResult.Found(viewName, view);
471474
}
472475

src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageResultExecutor.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class PageResultExecutor : ViewExecutor
1919
{
2020
private readonly IRazorViewEngine _razorViewEngine;
2121
private readonly IRazorPageActivator _razorPageActivator;
22+
private readonly DiagnosticSource _diagnosticSource;
2223
private readonly HtmlEncoder _htmlEncoder;
2324

2425
/// <summary>
@@ -42,6 +43,7 @@ public PageResultExecutor(
4243
_razorViewEngine = razorViewEngine;
4344
_htmlEncoder = htmlEncoder;
4445
_razorPageActivator = razorPageActivator;
46+
_diagnosticSource = diagnosticSource;
4547
}
4648

4749
/// <summary>
@@ -76,7 +78,8 @@ public virtual Task ExecuteAsync(PageContext pageContext, PageResult result)
7678
_razorPageActivator,
7779
viewStarts,
7880
new RazorPageAdapter(result.Page),
79-
_htmlEncoder);
81+
_htmlEncoder,
82+
_diagnosticSource);
8083

8184
return ExecuteAsync(viewContext, result.ContentType, result.StatusCode);
8285
}

test/Microsoft.AspNetCore.Mvc.Razor.Test/RazorViewEngineTest.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Diagnostics;
67
using System.Threading;
78
using Microsoft.AspNetCore.Hosting;
89
using Microsoft.AspNetCore.Http;
@@ -1350,7 +1351,8 @@ public void CreateCacheResult_LogsPrecompiledViewFound()
13501351
new HtmlTestEncoder(),
13511352
GetOptionsAccessor(expanders: null),
13521353
new FileProviderRazorProject(new TestFileProvider()),
1353-
loggerFactory);
1354+
loggerFactory,
1355+
new DiagnosticListener("Microsoft.AspNetCore.Mvc.Razor"));
13541356

13551357
// Act
13561358
var result = viewEngine.CreateCacheResult(null, relativePath, false);
@@ -1881,7 +1883,7 @@ public TestableRazorViewEngine(
18811883
IRazorPageFactoryProvider pageFactory,
18821884
IOptions<RazorViewEngineOptions> optionsAccessor,
18831885
RazorProject razorProject)
1884-
: base(pageFactory, Mock.Of<IRazorPageActivator>(), new HtmlTestEncoder(), optionsAccessor, razorProject, NullLoggerFactory.Instance)
1886+
: base(pageFactory, Mock.Of<IRazorPageActivator>(), new HtmlTestEncoder(), optionsAccessor, razorProject, NullLoggerFactory.Instance, new DiagnosticListener("Microsoft.AspNetCore.Mvc.Razor"))
18851887
{
18861888
}
18871889

0 commit comments

Comments
 (0)