【问题标题】:How to implement conditions for logging levels in the log attribute class in a web API project?如何在Web API项目的日志属性类中实现日志级别的条件?
【发布时间】:2019-08-27 06:47:12
【问题描述】:

在我的 Web API 项目中,我创建了一个继承自 ActionFilterAttribute 的 MyFilterAttribute 类。

我已经提到了 HttpMethod GET 的日志消息如下

using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;

namespace Resume
{
    public class MyFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            if (actionContext.Request.Method == HttpMethod.Get)
            {
                var log = log4net.LogManager.GetLogger(typeof(ApiController));
                log.Info("This is log meassage for GET method");
                log.Warn("This is a warn log message for a GET method");
            }

        }
    }
}

我在 EmployeeController 中的 GET 方法是这样的:

[Route("{eid}")]
[HttpGet]
[MyFilterAttribute]
public HttpResponseMessage GetEmployee(int eid)
  {
     var employee = _unitOfWork.Employee.GetById(eid);
     if (employee != null)
         return Request.CreateResponse(HttpStatusCode.OK, employee);
     else
         return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with Id " + eid + " does not exist");
        }

我在 CompanyController 中的 GET 方法是这样的:

[Route("{cid}")]
[HttpGet]
[MyFilterAttribute]
public HttpResponseMessage GetCompany(int cid)
  {
     var company = _unitOfWork.Company.GetById(eid);
     if (company != null)
         return Request.CreateResponse(HttpStatusCode.OK, company);
     else
         return Request.CreateErrorResponse(HttpStatusCode.NotFound, "company with Id " + cid + " does not exist");
        }

每当向任何控制器发出获取请求时,都会记录两个日志消息。

谁能帮忙提一下以不同日志级别登录不同控制器的条件

【问题讨论】:

    标签: c# visual-studio logging log4net


    【解决方案1】:

    您可以为LogLevel 创建一个Enum 然后使用它:

    public class MyFilterAttribute : ActionFilterAttribute
    {
        public LogLevel LogLevel { get; set; }
    
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            if (actionContext.Request.Method == HttpMethod.Get)
            {
                var log = log4net.LogManager.GetLogger(typeof(ApiController));
                if(LogLevel.Verbose)
                {
                   log.Info("This is log meassage for GET method");
                   log.Warn("This is a warn log message for a GET method");
                }
                if(LogLevel.Info)
                    log.Info("This is log meassage for GET method");
                if(LogLevel.Warning)
                     log.Warn("This is a warn log message for a GET method");
            }
    
        }
    }
    

    你的枚举会是这样的:

    public enum LogLevel
    {
         Information,
         Warning,
         Verbose
    } 
    

    将其用于以下操作:

    [MyFilterAttribute(LogLevel = LogLevel.Verbose)]
    public HttpResponseMessage GetCompany(int cid)
    {
    

    希望它能给你一些想法。

    【讨论】:

    • 记录器如何知道需要为控制器中的特定mothed选择哪个日志级别?
    • 通过在控制器方法上设置LogLevel参数
    猜你喜欢
    • 2017-08-26
    • 2013-11-28
    • 2016-08-06
    • 1970-01-01
    • 2018-06-27
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多