我正在尝试在我的 MVC 应用程序中创建自定义日志过滤器。以下是我的代码
public class LoggerAttribute: ActionFilterAttribute
{
private readonly IHttpLogService _httpLogService;
private readonly ILogService _logService;
public LoggerAttribute(IHttpLogService httpLogService, ILogService logService)
{
_httpLogService = httpLogService;
_logService = logService;
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
LogDetails(filterContext);
}
private void LogDetails(ActionExecutingContext filterContext)
{
try
{
HttpLogService httpService = new HttpLogService();
var httplogger = new LogMetaData()
{
RequestParams = filterContext,
ResponseParams = filterContext
};
_httpLogService.Emit("source", "", "Name", httplogger);
}
catch (Exception ex)
{
_logService.Emit("Error", "token", "Error encountered while trying to execute the request.", ex);
throw new Exception("An error occurred. Please try again later.");
}
}
}
在上面的代码中,我试图将服务实例传递给我的过滤器属性。如何实现将实例传递给我的自定义过滤器属性?