gpt4 book ai didi

asp.net-mvc - 仅将 MiniProfiler 用于调试或本地请求

转载 作者:行者123 更新时间:2023-12-02 05:55:30 25 4
gpt4 key购买 nike

我想在我的 MVC3 应用程序中使用 MiniProfiler,所以我遵循 Scott Hanselman's blog post

我的 Global.asax.cs 文件进行了必要的更改,如 source's MVC sample 中所示.

但我想测量我的 Controller 中的特定调用。所以我将这段代码放入 Controller 中:

if (Request.IsLocal)
{
var profiler = MiniProfiler.Current;
using (profiler.Step("SelectUserDetail Function"))
{
user = UserService.SelectUserDetail(userId);
}
}

我怀疑我的代码永远不会出现在生产环境中,因为我将此 block 包装在 Request.IsLocal 检查中。

如何仅针对本地调用或在 Debug模式下运行进行此检查?在任何情况下,它都应该执行 user = UserService.SelectUserDetail(userId)声明。

最佳答案

如果我正确理解你的问题,你只想调用 MiniProfiler 的 .Step() extension method在本地运行(或调试)时,正确吗?

如果是这样,这有点违背了 MiniProfiler 的目的,即让所有这些工具可用于生产代码,不影响生产

我相信您可以在代码中简单地执行此操作:

using (MiniProfiler.Current.Step("SelectUserDetail Function"))
{
user = UserService.SelectUserDetail(userId);
}

它几乎不会对您的应用产生任何影响;我们在 Stack Overflow 上的代码中确实执行了数百次,没有任何问题(以及每个数据库查询)。

您应该只需要在收到新请求时进行检查:

protected void Application_BeginRequest()
{
if (Request.IsLocal) { MiniProfiler.Start(); }
}

当您在生产环境中运行时,对 MiniProfiler.Current.Step() 的任何调用都不会返回任何内容,因为探查器为 null(扩展方法的美妙之处)。

如果您仍然想防止任何 using 语句出现在您的生产代码中,您应该熟悉 preprocessor directives 。请参阅this question ,还有。然而,我强烈建议他们不要这样做,因为这是没有必要的。

关于asp.net-mvc - 仅将 MiniProfiler 用于调试或本地请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14458790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com