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

[Design] Add DiagnosticSource to RazorView #6386

Merged
merged 4 commits into from
Jun 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Diagnostics;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Microsoft.AspNetCore.Mvc.Razor.Internal
{
public static class MvcRazorDiagnosticSourceExtensions
{
public static void BeforeViewPage(
this DiagnosticSource diagnosticSource,
IRazorPage page,
ViewContext viewContext)
{
if (diagnosticSource.IsEnabled("Microsoft.AspNetCore.Mvc.Razor.BeforeViewPage"))
{
diagnosticSource.Write(
"Microsoft.AspNetCore.Mvc.Razor.BeforeViewPage",
new
{
page = page,
viewContext = viewContext,
actionDescriptor = viewContext.ActionDescriptor,
httpContext = viewContext.HttpContext,
});
}
}

public static void AfterViewPage(
this DiagnosticSource diagnosticSource,
IRazorPage page,
ViewContext viewContext)
{
if (diagnosticSource.IsEnabled("Microsoft.AspNetCore.Mvc.Razor.AfterViewPage"))
{
diagnosticSource.Write(
"Microsoft.AspNetCore.Mvc.Razor.AfterViewPage",
new
{
page = page,
viewContext = viewContext,
actionDescriptor = viewContext.ActionDescriptor,
httpContext = viewContext.HttpContext,
});
}
}
}
}
26 changes: 23 additions & 3 deletions src/Microsoft.AspNetCore.Mvc.Razor/RazorView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewEngines;
using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal;
using Microsoft.AspNetCore.Mvc.Razor.Internal;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.AspNetCore.Mvc.Razor
Expand All @@ -23,6 +24,7 @@ public class RazorView : IView
private readonly IRazorViewEngine _viewEngine;
private readonly IRazorPageActivator _pageActivator;
private readonly HtmlEncoder _htmlEncoder;
private readonly DiagnosticSource _diagnosticSource;
private IViewBufferScope _bufferScope;

/// <summary>
Expand All @@ -34,12 +36,14 @@ public class RazorView : IView
/// </param>
/// <param name="razorPage">The <see cref="IRazorPage"/> instance to execute.</param>
/// <param name="htmlEncoder">The HTML encoder.</param>
/// <param name="diagnosticSource">The <see cref="DiagnosticSource"/>.</param>
public RazorView(
IRazorViewEngine viewEngine,
IRazorPageActivator pageActivator,
IReadOnlyList<IRazorPage> viewStartPages,
IRazorPage razorPage,
HtmlEncoder htmlEncoder)
HtmlEncoder htmlEncoder,
DiagnosticSource diagnosticSource)
{
if (viewEngine == null)
{
Expand All @@ -66,11 +70,17 @@ public RazorView(
throw new ArgumentNullException(nameof(htmlEncoder));
}

if (diagnosticSource == null)
{
throw new ArgumentNullException(nameof(diagnosticSource));
}

_viewEngine = viewEngine;
_pageActivator = pageActivator;
ViewStartPages = viewStartPages;
RazorPage = razorPage;
_htmlEncoder = htmlEncoder;
_diagnosticSource = diagnosticSource;
}

/// <inheritdoc />
Expand Down Expand Up @@ -152,11 +162,21 @@ private async Task<ViewBufferTextWriter> RenderPageAsync(
}
}

private Task RenderPageCoreAsync(IRazorPage page, ViewContext context)
private async Task RenderPageCoreAsync(IRazorPage page, ViewContext context)
{
page.ViewContext = context;
_pageActivator.Activate(page, context);
return page.ExecuteAsync();

_diagnosticSource.BeforeViewPage(page, context);

try
{
await page.ExecuteAsync();
}
finally
{
_diagnosticSource.AfterViewPage(page, context);
}
}

private async Task RenderViewStartsAsync(ViewContext context)
Expand Down
7 changes: 5 additions & 2 deletions src/Microsoft.AspNetCore.Mvc.Razor/RazorViewEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class RazorViewEngine : IRazorViewEngine
private readonly ILogger _logger;
private readonly RazorViewEngineOptions _options;
private readonly RazorProject _razorProject;
private readonly DiagnosticSource _diagnosticSource;

/// <summary>
/// Initializes a new instance of the <see cref="RazorViewEngine" />.
Expand All @@ -54,7 +55,8 @@ public RazorViewEngine(
HtmlEncoder htmlEncoder,
IOptions<RazorViewEngineOptions> optionsAccessor,
RazorProject razorProject,
ILoggerFactory loggerFactory)
ILoggerFactory loggerFactory,
DiagnosticSource diagnosticSource)
{
_options = optionsAccessor.Value;

Expand All @@ -77,6 +79,7 @@ public RazorViewEngine(
_htmlEncoder = htmlEncoder;
_logger = loggerFactory.CreateLogger<RazorViewEngine>();
_razorProject = razorProject;
_diagnosticSource = diagnosticSource;
ViewLookupCache = new MemoryCache(new MemoryCacheOptions());
}

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

var view = new RazorView(this, _pageActivator, viewStarts, page, _htmlEncoder);
var view = new RazorView(this, _pageActivator, viewStarts, page, _htmlEncoder, _diagnosticSource);
return ViewEngineResult.Found(viewName, view);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class PageResultExecutor : ViewExecutor
{
private readonly IRazorViewEngine _razorViewEngine;
private readonly IRazorPageActivator _razorPageActivator;
private readonly DiagnosticSource _diagnosticSource;
private readonly HtmlEncoder _htmlEncoder;

/// <summary>
Expand All @@ -42,6 +43,7 @@ public PageResultExecutor(
_razorViewEngine = razorViewEngine;
_htmlEncoder = htmlEncoder;
_razorPageActivator = razorPageActivator;
_diagnosticSource = diagnosticSource;
}

/// <summary>
Expand Down Expand Up @@ -76,7 +78,8 @@ public virtual Task ExecuteAsync(PageContext pageContext, PageResult result)
_razorPageActivator,
viewStarts,
new RazorPageAdapter(result.Page),
_htmlEncoder);
_htmlEncoder,
_diagnosticSource);

return ExecuteAsync(viewContext, result.ContentType, result.StatusCode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
Expand Down Expand Up @@ -1350,7 +1351,8 @@ public void CreateCacheResult_LogsPrecompiledViewFound()
new HtmlTestEncoder(),
GetOptionsAccessor(expanders: null),
new FileProviderRazorProject(new TestFileProvider()),
loggerFactory);
loggerFactory,
new DiagnosticListener("Microsoft.AspNetCore.Mvc.Razor"));

// Act
var result = viewEngine.CreateCacheResult(null, relativePath, false);
Expand Down Expand Up @@ -1881,7 +1883,7 @@ public TestableRazorViewEngine(
IRazorPageFactoryProvider pageFactory,
IOptions<RazorViewEngineOptions> optionsAccessor,
RazorProject razorProject)
: base(pageFactory, Mock.Of<IRazorPageActivator>(), new HtmlTestEncoder(), optionsAccessor, razorProject, NullLoggerFactory.Instance)
: base(pageFactory, Mock.Of<IRazorPageActivator>(), new HtmlTestEncoder(), optionsAccessor, razorProject, NullLoggerFactory.Instance, new DiagnosticListener("Microsoft.AspNetCore.Mvc.Razor"))
{
}

Expand Down
Loading