-
Notifications
You must be signed in to change notification settings - Fork 608
ASP.NET Core 3.x: use Diagnostic bits #475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
183 changes: 183 additions & 0 deletions
183
src/MiniProfiler.AspNetCore.Mvc/MvcDiagnosticListener.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
#if NETCOREAPP3_0 | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Abstractions; | ||
using Microsoft.AspNetCore.Mvc.Diagnostics; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using StackExchange.Profiling.Internal; | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
|
||
namespace StackExchange.Profiling.Data | ||
{ | ||
/// <summary> | ||
/// Diagnostic listener for Microsoft.AspNetCore.Mvc.* events | ||
/// </summary> | ||
/// <remarks> | ||
/// See: | ||
/// - https://github.com/dotnet/aspnetcore/blob/v3.0.0/src/Mvc/Mvc.Core/src/MvcCoreDiagnosticListenerExtensions.cs | ||
/// - https://github.com/dotnet/aspnetcore/blob/v3.0.0/src/Mvc/Mvc.Core/src/Diagnostics/MvcDiagnostics.cs | ||
/// - https://github.com/dotnet/aspnetcore/blob/v3.0.0/src/Mvc/Mvc.Razor/src/Diagnostics/MvcDiagnostics.cs | ||
/// </remarks> | ||
public class MvcDiagnosticListener : IMiniProfilerDiagnosticListener | ||
{ | ||
/// <summary> | ||
/// Diagnostic Listener name to handle. | ||
/// </summary> | ||
public string ListenerName => "Microsoft.AspNetCore"; | ||
|
||
/// <summary> | ||
/// Notifies the observer that the provider has finished sending push-based notifications. | ||
/// </summary> | ||
public void OnCompleted() { } | ||
|
||
/// <summary> | ||
/// Notifies the observer that the provider has experienced an error condition. | ||
/// </summary> | ||
/// <param name="error">An object that provides additional information about the error.</param> | ||
public void OnError(Exception error) => Trace.WriteLine(error); | ||
|
||
// So we don't keep allocating strings for the same actions over and over | ||
private readonly ConcurrentDictionary<(string, string), string> _descriptorNameCache = new ConcurrentDictionary<(string, string), string>(); | ||
|
||
private string GetName(string label, ActionDescriptor descriptor) | ||
{ | ||
var key = (label, descriptor.DisplayName); | ||
if (!_descriptorNameCache.TryGetValue(key, out var result)) | ||
{ | ||
// For the "Samples.AspNetCore.Controllers.HomeController.Index (Samples.AspNetCore3)" format, | ||
// ...trim off the assembly on the end. | ||
var assemblyNamePos = descriptor.DisplayName.IndexOf(" ("); | ||
if (assemblyNamePos > 0) | ||
{ | ||
result = string.Concat(label, ": ", descriptor.DisplayName.AsSpan().Slice(0, assemblyNamePos)); | ||
} | ||
else | ||
{ | ||
result = label + ": " + descriptor.DisplayName; | ||
} | ||
_descriptorNameCache[key] = result; | ||
} | ||
return result; | ||
} | ||
|
||
private static string GetName(IActionResult result) => result switch | ||
{ | ||
ViewResult vr => vr.ViewName.HasValue() ? "View: " + vr.ViewName : nameof(ViewResult), | ||
ContentResult cr => cr.ContentType.HasValue() ? "Content: " + cr.ContentType : nameof(ContentResult), | ||
ObjectResult or => or.DeclaredType != null ? "Object: " + or.DeclaredType.Name : nameof(ObjectResult), | ||
StatusCodeResult scr => scr.StatusCode > 0 ? "Status Code: " + scr.StatusCode.ToString() : nameof(StatusCodeResult), | ||
JsonResult jr => jr.ContentType.HasValue() ? "JSON: " + jr.ContentType : nameof(JsonResult), | ||
_ => "Result: " + result.GetType().Name | ||
}; | ||
|
||
private static string GetName(IFilterMetadata filter) => filter.GetType().Name; | ||
|
||
/// <summary> | ||
/// Stores the current timing in the tree, on each request. | ||
/// </summary> | ||
private readonly AsyncLocal<(object State, Timing Timing)> CurrentTiming = new AsyncLocal<(object, Timing)>(); | ||
|
||
private object Start<T>(T state, string stepName) where T : class | ||
{ | ||
CurrentTiming.Value = (state, MiniProfiler.Current.Step(stepName)); | ||
return null; | ||
} | ||
|
||
private object Complete<T>(T state) where T : class | ||
{ | ||
if (CurrentTiming.Value.State is T currentState && currentState == state) | ||
{ | ||
using (CurrentTiming.Value.Timing) { } | ||
} | ||
return null; | ||
} | ||
|
||
/// <summary> | ||
/// Provides the observer with new data. | ||
/// </summary> | ||
/// <param name="kv">The current notification information.</param> | ||
public void OnNext(KeyValuePair<string, object> kv) => _ = kv.Value switch | ||
{ | ||
// MVC Bits: https://github.com/dotnet/aspnetcore/blob/v3.0.0/src/Mvc/Mvc.Core/src/Diagnostics/MvcDiagnostics.cs | ||
// ActionEvent | ||
BeforeActionEventData data => Start(data.ActionDescriptor, GetName("Action", data.ActionDescriptor)), | ||
AfterActionEventData data => Complete(data.ActionDescriptor), | ||
// ControllerActionMethod | ||
BeforeControllerActionMethodEventData data => Start(data.ActionContext.ActionDescriptor, GetName("Controller Action", data.ActionContext.ActionDescriptor)), | ||
AfterControllerActionMethodEventData data => Complete(data.ActionContext.ActionDescriptor), | ||
// ActionResultEvent | ||
BeforeActionResultEventData data => Start(data.Result, GetName(data.Result)), | ||
AfterActionResultEventData data => Complete(data.Result), | ||
|
||
// AuthorizationFilterOnAuthorization | ||
BeforeAuthorizationFilterOnAuthorizationEventData data => Start(data.Filter, "Auth Filter: " + GetName(data.Filter)), | ||
AfterAuthorizationFilterOnAuthorizationEventData data => Complete(data.Filter), | ||
|
||
// ResourceFilterOnResourceExecution | ||
BeforeResourceFilterOnResourceExecutionEventData data => Start(data.Filter, "Resource Filter (Exec): " + GetName(data.Filter)), | ||
AfterResourceFilterOnResourceExecutionEventData data => Complete(data.Filter), | ||
// ResourceFilterOnResourceExecuting | ||
BeforeResourceFilterOnResourceExecutingEventData data => Start(data.Filter, "Resource Filter (Execing): " + GetName(data.Filter)), | ||
AfterResourceFilterOnResourceExecutingEventData data => Complete(data.Filter), | ||
// ResourceFilterOnResourceExecuted | ||
BeforeResourceFilterOnResourceExecutedEventData data => Start(data.Filter, "Resource Filter (Execed): " + GetName(data.Filter)), | ||
AfterResourceFilterOnResourceExecutedEventData data => Complete(data.Filter), | ||
|
||
// ExceptionFilterOnException | ||
BeforeExceptionFilterOnException data => Start(data.Filter, "Exception Filter: " + GetName(data.Filter)), | ||
AfterExceptionFilterOnExceptionEventData data => Complete(data.Filter), | ||
|
||
// ActionFilterOnActionExecution | ||
BeforeActionFilterOnActionExecutionEventData data => Start(data.Filter, "Action Filter (Exec): " + GetName(data.Filter)), | ||
AfterActionFilterOnActionExecutionEventData data => Complete(data.Filter), | ||
// ActionFilterOnActionExecuting | ||
BeforeActionFilterOnActionExecutingEventData data => Start(data.Filter, "Action Filter (Execing): " + GetName(data.Filter)), | ||
AfterActionFilterOnActionExecutingEventData data => Complete(data.Filter), | ||
// ActionFilterOnActionExecuted | ||
BeforeActionFilterOnActionExecutedEventData data => Start(data.Filter, "Action Filter (Execed): " + GetName(data.Filter)), | ||
AfterActionFilterOnActionExecutedEventData data => Complete(data.Filter), | ||
|
||
// ResultFilterOnResultExecution | ||
BeforeResultFilterOnResultExecutionEventData data => Start(data.Filter, "Result Filter (Exec): " + GetName(data.Filter)), | ||
AfterResultFilterOnResultExecutionEventData data => Complete(data.Filter), | ||
// ResultFilterOnResultExecuting | ||
BeforeResultFilterOnResultExecutingEventData data => Start(data.Filter, "Result Filter (Execing): " + GetName(data.Filter)), | ||
AfterResultFilterOnResultExecutingEventData data => Complete(data.Filter), | ||
// ResultFilterOnResultExecuted | ||
BeforeResultFilterOnResultExecutedEventData data => Start(data.Filter, "Result Filter (Execed): " + GetName(data.Filter)), | ||
AfterResultFilterOnResultExecutedEventData data => Complete(data.Filter), | ||
|
||
// Razor Bits: https://github.com/dotnet/aspnetcore/blob/v3.0.0/src/Mvc/Mvc.Razor/src/Diagnostics/MvcDiagnostics.cs | ||
// ViewPage | ||
BeforeViewPageEventData data => Start(data.Page, "View: " + data.Page.Path), | ||
AfterViewPageEventData data => Complete(data.Page), | ||
|
||
// RazorPage Bits: https://github.com/dotnet/aspnetcore/blob/v3.0.0/src/Mvc/Mvc.RazorPages/src/Diagnostics/MvcDiagnostics.cs | ||
// HandlerMethod | ||
BeforeHandlerMethodEventData data => Start(data.Instance, "Handler: " + data.HandlerMethodDescriptor.Name), | ||
AfterHandlerMethodEventData data => Complete(data.Instance), | ||
|
||
// PageFilterOnPageHandlerExecution | ||
BeforePageFilterOnPageHandlerExecutionEventData data => Start(data.Filter, "Filter (Exec): " + GetName(data.Filter)), | ||
AfterPageFilterOnPageHandlerExecutionEventData data => Complete(data.Filter), | ||
// PageFilterOnPageHandlerExecuting | ||
BeforePageFilterOnPageHandlerExecutingEventData data => Start(data.Filter, "Filter (Execing): " + GetName(data.Filter)), | ||
AfterPageFilterOnPageHandlerExecutingEventData data => Complete(data.Filter), | ||
// PageFilterOnPageHandlerExecuted | ||
BeforePageFilterOnPageHandlerExecutedEventData data => Start(data.Filter, "Filter (Execed): " + GetName(data.Filter)), | ||
AfterPageFilterOnPageHandlerExecutedEventData data => Complete(data.Filter), | ||
|
||
// PageFilterOnPageHandlerSelection | ||
BeforePageFilterOnPageHandlerSelectionEventData data => Start(data.Filter, "Filter (Selection): " + GetName(data.Filter)), | ||
AfterPageFilterOnPageHandlerSelectionEventData data => Complete(data.Filter), | ||
// PageFilterOnPageHandlerSelected | ||
BeforePageFilterOnPageHandlerSelectedEventData data => Start(data.Filter, "Filter (Selected): " + GetName(data.Filter)), | ||
AfterPageFilterOnPageHandlerSelectedEventData data => Complete(data.Filter), | ||
_ => null | ||
}; | ||
} | ||
} | ||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.