【发布时间】: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