【发布时间】:2017-03-09 14:00:07
【问题描述】:
我正在构建一个 ASP.NET Core MVC 应用程序,并尝试创建一个全局操作过滤器,用于记录执行操作所花费的时间(仅当花费的时间超过某个阈值时才应该记录)。我已经成功地做到了这一点,但现在我想说单个动作或单个控制器应该有不同的阈值。当我尝试这个时,我的动作过滤器被应用了两次(这不是我想要的),但使用了正确的两个不同的阈值。
我已经尝试了很多东西并四处搜索。在 MVC 3 和 MVC 4 项目中,我已经使用 Global.asax 中的 RegisterGlobalFilters() 成功完成了这项工作,当我在控制器/动作上使用该属性时,它会自动覆盖全局。我也尝试了这篇文章中列出的方法,但没有运气:
Override global authorize filter in ASP.NET Core MVC 1.0
我的 ActionFilterAttribute 代码:
public class PerformanceLoggingAttribute : ActionFilterAttribute
{
public int ExpectedMax = -1; // Log everything unless this is explicitly set
private Stopwatch sw;
public override void OnActionExecuting(ActionExecutingContext context)
{
sw = Stopwatch.StartNew();
}
public override void OnActionExecuted(ActionExecutedContext context)
{
sw.Stop();
if (sw.ElapsedMilliseconds >= ExpectedMax)
{
// Log here
}
}
//public override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
//{
// // If there is another performance filter, do nothing
// if (context.Filters.Any(item => item is PerformanceLoggingAttribute && item != this))
// {
// return Task.FromResult(0);
// }
// return base.OnActionExecutionAsync(context, next);
//}
}
我在我的 Startup.cs 中应用这个全局过滤器:
services.AddMvc(options =>
{
if (_env.IsProduction()) options.Filters.Add(new RequireHttpsAttribute());
//options.Filters.Add(new PerformanceLoggingFilter() { ExpectedMax = 1 }); // Add Performance Logging filter
options.Filters.Add(new PerformanceLoggingAttribute() { ExpectedMax = 1 }); // Add Performance Logging filter
});
在我的控制器中,我正在应用属性:
//[TypeFilter(typeof(PerformanceLoggingFilter))]
[PerformanceLogging(ExpectedMax = 2)]
public IActionResult Index()
{
var vm = _performanceBuilder.BuildPerformanceViewModel();
return View(vm);
}
从上面的代码 sn-ps 可以看出,我尝试了 OnActionExecutionAsync 方法,我也尝试了 IActionFilter 并在操作上使用 [TypeFilter(typeof(PerformanceLoggingFilter))],但没有运气。
谁能帮帮我?
【问题讨论】:
标签: c# asp.net-core asp.net-core-mvc