diff --git a/src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs b/src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs
index 266f072555..b03ff33284 100644
--- a/src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs
+++ b/src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs
@@ -84,6 +84,12 @@ namespace Microsoft.AspNet.Mvc.Razor
///
public IPageExecutionContext PageExecutionContext { get; set; }
+ ///
+ /// Gets or sets a instance used to instrument the page execution.
+ ///
+ [RazorInject]
+ public DiagnosticSource DiagnosticSource { get; set; }
+
///
/// Gets the that the page is writing output to.
///
@@ -1063,12 +1069,41 @@ namespace Microsoft.AspNet.Mvc.Razor
public void BeginContext(int position, int length, bool isLiteral)
{
+ const string BeginContextEvent = "Microsoft.AspNet.Mvc.Razor.BeginInstrumentationContext";
+
PageExecutionContext?.BeginContext(position, length, isLiteral);
+ if (DiagnosticSource?.IsEnabled(BeginContextEvent) == true)
+ {
+ DiagnosticSource.Write(
+ BeginContextEvent,
+ new
+ {
+ httpContext = Context,
+ path = Path,
+ isPartial = IsPartial,
+ position = position,
+ length = length,
+ isLiteral = isLiteral,
+ });
+ }
}
public void EndContext()
{
+ const string EndContextEvent = "Microsoft.AspNet.Mvc.Razor.EndInstrumentationContext";
+
PageExecutionContext?.EndContext();
+ if (DiagnosticSource?.IsEnabled(EndContextEvent) == true)
+ {
+ DiagnosticSource.Write(
+ EndContextEvent,
+ new
+ {
+ httpContext = Context,
+ path = Path,
+ isPartial = IsPartial,
+ });
+ }
}
///
diff --git a/test/Microsoft.AspNet.Mvc.Razor.Test/RazorPageActivatorTest.cs b/test/Microsoft.AspNet.Mvc.Razor.Test/RazorPageActivatorTest.cs
index 93e4586647..636e1f45cd 100644
--- a/test/Microsoft.AspNet.Mvc.Razor.Test/RazorPageActivatorTest.cs
+++ b/test/Microsoft.AspNet.Mvc.Razor.Test/RazorPageActivatorTest.cs
@@ -2,11 +2,13 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
+using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
+using Microsoft.AspNet.Http.Internal;
using Microsoft.AspNet.Mvc.Abstractions;
using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Mvc.Razor.Internal;
@@ -15,6 +17,7 @@ using Microsoft.AspNet.Mvc.ViewEngines;
using Microsoft.AspNet.Mvc.ViewFeatures;
using Microsoft.AspNet.Mvc.ViewFeatures.Internal;
using Microsoft.AspNet.Routing;
+using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.WebEncoders.Testing;
using Moq;
using Xunit;
@@ -33,24 +36,26 @@ namespace Microsoft.AspNet.Mvc.Razor
var myService = new MyService();
var helper = Mock.Of>();
var htmlEncoder = new HtmlTestEncoder();
- var serviceProvider = new Mock();
- serviceProvider.Setup(p => p.GetService(typeof(MyService)))
- .Returns(myService);
- serviceProvider.Setup(p => p.GetService(typeof(IHtmlHelper