ASP.NET核心筛选器-忽略定义的方法 [英] ASP.NET Core Filters - ignore for defined methods

查看:92
本文介绍了ASP.NET核心筛选器-忽略定义的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们将日志信息实施到我们的数据库中. 我将为其使用过滤器(IActionFilter)功能. 我写了以下课程:

We implement a log information to our database. I will use a Filters (IActionFilter) functionality for it. I wrote the following class:

public class ActionFilter: Attribute, IActionFilter
{
    DateTime start;
    public void OnActionExecuting(ActionExecutingContext context)
    {
        start = DateTime.Now;
    }

    public void OnActionExecuted(ActionExecutedContext context)
    {
        DateTime end = DateTime.Now;
        double processTime = end.Subtract(start).TotalMilliseconds;
        ... some log actions
    }
}

然后我将以下代码添加到Startup.cs:

Then I have added the following code to the Startup.cs:

services.AddMvc(options => {
            options.Filters.Add(typeof(ActionFilter));

        });

工作正常.我在每个方法的ActionFilter中都有一个断点.

It works fine. I get the breakpoint in ActionFilter for each my method.

但是我想忽略大部分方法的日志记录. 据我了解,我可以使用自己的属性来做到这一点.我以前没有使用自己的属性. 好的,我写了以下属性:

But I want to ignore for logging the most part of methods. As I understand, I can do it with my own attribute. I didn't work with own attributes before. Ok, I wrote the following attribute:

public class IgnoreAttribute : Attribute
{
    public IgnoreAttribute()
    { }
}

我将属性添加到方法:

[Ignore]
    [HttpGet]
    [Route("api/AppovedTransactionAmountByDays/{daysCount}")]
    public JsonResult GetAppovedTransactionAmountByDays(int daysCount)
    {
        var result = daysCount;

        return new JsonResult(result);
    }

当然,简单的操作是行不通的.

Of course, there simple actions don't work.

我必须如何更改属性或ActionFilter才能忽略方法?

How I must change my attribute or my ActionFilter for ignore of method?

谢谢.

推荐答案

felix-b关于命名的注释是一个很好的注释.

felix-b's note about naming is a good one.

我想再记一下.以这种方式注册状态时,不应将状态存储在过滤器中.由于它是一个属性,因此只能实例化一次!因此,您那里的比赛条件繁重.一种选择是使用:

And I want to make another note. You should not store state in the filter when you register it in this way. Since it is an attribute, it is instantiated only once! So you have a massive race condition there. One option would be to use:

services.AddMvc(o =>
{
    o.Filters.Add(new ServiceFilterAttribute(typeof(LoggingActionFilter)));
});

并将其注册为瞬态:

services.AddTransient<LoggingActionFilter>();

现在,该属性在每次需要时都会实例化,因此您可以安全地存储状态.

Now the attribute is instantiated every time it is needed, so you can safely store state.

也可以对其进行配置,以便在存在 marker属性的情况下忽略操作:

Configuring it so that it ignores the action if the marker attribute is present is also possible:

public class LoggingActionFilter : Attribute, IActionFilter
{
    private DateTime start;
    private bool skipLogging = false;

    public void OnActionExecuting(ActionExecutingContext context)
    {
        var descriptor = (ControllerActionDescriptor)context.ActionDescriptor;
        var attributes = descriptor.MethodInfo.CustomAttributes;

        if (attributes.Any(a => a.AttributeType == typeof(SkipLoggingAttribute)))
        {
            skipLogging = true;
            return;
        }

        start = DateTime.Now;
    }

    public void OnActionExecuted(ActionExecutedContext context)
    {
        if (skipLogging)
        {
            return;
        }

        DateTime end = DateTime.Now;
        double processTime = end.Subtract(start).TotalMilliseconds;
    }
}

public class SkipLoggingAttribute : Attribute
{
}

在这里,我们从参数中获得可用的动作描述符,并确定所讨论的方法是否具有SkipLogging属性.如果是这样,请跳过日志记录代码.

Here we get the action descriptor available from the parameter and find if the method in question has the SkipLogging attribute. If it does, skip the logging code.

这篇关于ASP.NET核心筛选器-忽略定义的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆