我可以在 PostAuthorizeRequest 方法中启动 MVC Mini Profiler 吗?
我正在使用 MVC Mini Profiler,并且仅向处于“Profiler”角色的经过身份验证的用户显示探查器。 MiniProfiler.cs 中的示例使用 AuthenticateRequest 方法来确定是否应该停止分析,但我将我的方法切换为使用 PostAuthorizeRequest (在阅读 这个问题),以便我可以访问 IPrincipal和 IsInRole 方法。我可以在 PostAuthorizeRequest 方法中启动探查器,还是应该继续停止并丢弃 PostAuthorizeRequest 中的结果?针对每个请求启动和停止分析器的开销是多少?
当前代码:
public void Init(HttpApplication context)
{
context.BeginRequest += (sender, e) =>
{
MiniProfiler.Start();
};
context.PostAuthorizeRequest += (sender, e) =>
{
var user = ((HttpApplication)sender).Context.User;
if (user == null || !user.Identity.IsAuthenticated || !user.IsInRole("Profiler"))
{
MvcMiniProfiler.MiniProfiler.Stop(discardResults: true);
}
};
context.EndRequest += (sender, e) =>
{
MiniProfiler.Stop();
};
}
提议代码:
public void Init(HttpApplication context)
{
context.PostAuthorizeRequest += (sender, e) =>
{
var user = ((HttpApplication)sender).Context.User;
if (user != null && user.Identity.IsAuthenticated && user.IsInRole("Profiler"))
{
MiniProfiler.Start();
}
};
context.EndRequest += (sender, e) =>
{
MiniProfiler.Stop();
};
}
I'm using the MVC Mini Profiler and I'm only showing the profiler for authenticated users who are in the "Profiler" role. The example shipping in MiniProfiler.cs was using the AuthenticateRequest method to determine if it should stop profiling, but I switched mine to use the PostAuthorizeRequest (after reading this question) so that I could get access to the IPrincipal and IsInRole method. Can I just start the profiler in the PostAuthorizeRequest method, or should I continue to Stop and discard the results in the PostAuthorizeRequest? What is the overhead to starting and stopping the profiler for every request?
Current Code:
public void Init(HttpApplication context)
{
context.BeginRequest += (sender, e) =>
{
MiniProfiler.Start();
};
context.PostAuthorizeRequest += (sender, e) =>
{
var user = ((HttpApplication)sender).Context.User;
if (user == null || !user.Identity.IsAuthenticated || !user.IsInRole("Profiler"))
{
MvcMiniProfiler.MiniProfiler.Stop(discardResults: true);
}
};
context.EndRequest += (sender, e) =>
{
MiniProfiler.Stop();
};
}
Proposed Code:
public void Init(HttpApplication context)
{
context.PostAuthorizeRequest += (sender, e) =>
{
var user = ((HttpApplication)sender).Context.User;
if (user != null && user.Identity.IsAuthenticated && user.IsInRole("Profiler"))
{
MiniProfiler.Start();
}
};
context.EndRequest += (sender, e) =>
{
MiniProfiler.Stop();
};
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以随时使用以下调用放弃您的分析结果:
在 StackOverflow,我们的“高性能”模式是:
Application_BeginRequest
中找到 cookie - MiniProfiler.Start();PostAuthorizeRequest
之后:您的目标始终是尽早开始分析,并尽可能晚地停止分析。如果仅从管道的中间开始,则不会分析可能存在瓶颈的管道部分。
You can abandon your profiling results at any time using the call:
At StackOverflow our "high performance" pattern is:
Application_BeginRequest
- MiniProfiler.Start();PostAuthorizeRequest
:Your goal is always to start profiling as early as possible, and stop profiling as late as possible. If you only start in the middle of the pipeline, portions of the pipeline where bottlenecks may reside will not be profiled.
我认为尽早启动探查器很重要(否则您可能会丢失一些关键信息,例如身份验证过程是否需要一段时间,或者某些 HTTP 模块是否存在问题)。
由于
BeginRequest
事件先于请求发生的任何其他事件发生,因此它成为开始分析的理想位置,然后决定是否要在稍后的步骤中保留分析数据 (PostAuthorize
,就你的情况而言)。I think starting the profiler as early as possible is important (otherwise you could be missing some key information, such as if the authentication process it self takes a while, or if some HTTP module has issues).
Since the
BeginRequest
event occurs prior to anything else occurring with the request, this makes it the ideal place to begin profiling, then decide if you want to keep the profiled data at a later step (PostAuthorize
, in your case).