- 构建过滤器 ActionMonitorAttribute
/*
* --------------------------------------------------
* Namespace : ChaseWinServer.WebApiconfig
* Author : song
* CreateTime : 2022/10/10 10:36:02
* Remark : 可以拓展统一校验、日志记录、异常处理
* --------------------------------------------------
*/
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
namespace ChaseWinServer.WebApiconfig
{
public class ActionMonitorAttribute : ActionFilterAttribute
{
private static ILog log = log4net.LogManager.GetLogger(typeof(ActionMonitorAttribute));
/// <summary>
/// 客户端IP地址
/// </summary>
private String ClientIp { get; set; }
/// <summary>
/// 请求处理前
/// </summary>
/// <param name="actionContext"></param>
public override void OnActionExecuting(HttpActionContext actionContext)
{
//参数非空判断
if (!actionContext.ModelState.IsValid)
{
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, actionContext.ModelState);
}
dynamic RemoteEndpoint = null;
if (actionContext.Request.Properties.TryGetValue("System.ServiceModel.Channels.RemoteEndpointMessageProperty", out RemoteEndpoint))
{
ClientIp = RemoteEndpoint.Address;
if (ClientIp == "::1")
{
ClientIp = "localhost";
}
}
String Message = $"[ClientIp]:{ClientIp} [RequestUri]:{ actionContext.Request.RequestUri} [Method]:{actionContext.Request.Method} [Body]:{actionContext.Request.Content.ReadAsStringAsync().Result}";
log.Info("Request:" + Message);
base.OnActionExecuting(actionContext);
}
/// <summary>
/// 请求返回前
/// </summary>
/// <param name="actionExecutedContext"></param>
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if (actionExecutedContext.Exception == null)
{
string Message = $"[ClientIp]:{ClientIp} [ReasonPhrase]:{actionExecutedContext.Response.ReasonPhrase} [Data]:{actionExecutedContext.Response.Content.ReadAsStringAsync().Result}";
log.Info("Response:"+ Message);
}
else
{
//string Message = $"[ClientIp]:{ClientIp} Exception";
string Message = $"[WebApiRequest].[Exception] [RequestUri]:{actionExecutedContext.Request.RequestUri} [StackTrace]{actionExecutedContext.Exception.StackTrace}";
log.Info("Response:" + Message);
}
base.OnActionExecuted(actionExecutedContext);
}
}
}
2.创建FilterController 类 继承 ApiController,并添加ActionMonitor属性
/*
* --------------------------------------------------
* Namespace : ChaseWinServer.WebApiconfig
* Author : song
* CreateTime : 2022/10/10 10:23:52
* Remark :
* --------------------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
namespace ChaseWinServer.WebApiconfig
{
[ActionMonitor]
public class FilterController : ApiController
{
}
}
3.在需要过滤的Controller类更改继承为 FilterController
/*
* --------------------------------------------------
* Namespace : ChaseWinServer
* Author : song
* CreateTime : 2022/10/9 16:54:20
* Remark :
* --------------------------------------------------
*/
using ChaseWinServer.WebApiconfig;
using log4net;
using System.Web.Http;
namespace ChaseWinServer
{
public class TestController : FilterController
{
private static ILog log = log4net.LogManager.GetLogger(typeof(TestController));
[HttpGet]
public string Add()
{
log.Info("add");
return "你好";
}
}
}
4.测试过滤器日志打印