c# Attribute

public class LogIPAttribute : TypeFilterAttribute
{
    public LogIPAttribute() : base(typeof(LogIPFilterImpl))
    {
    }
    public class LogIPFilterImpl(ILogger<LogIPFilterImpl> logger) : IActionFilter
    {
        public void OnActionExecuting(ActionExecutingContext context)
        {
            var ipAddress = context.HttpContext.Connection.RemoteIpAddress?.ToString();
            var httpMethod = context.HttpContext.Request.Method;
            logger.LogInformation($"Accessed IP: {ipAddress}, Method: {httpMethod}");
        }

        public void OnActionExecuted(ActionExecutedContext context)
        {

        }
    }
}

posted @ 2024-02-21 21:52  月渊  阅读(3)  评论(0编辑  收藏  举报