Created
July 8, 2018 05:37
-
-
Save danielplawgo/93a7f3c699da9073d7c9fa1ae30c584f to your computer and use it in GitHub Desktop.
Log using action filters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FilterConfig | |
{ | |
public static void RegisterGlobalFilters(GlobalFilterCollection filters) | |
{ | |
filters.Add(new LogFilterAttribute()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LogFilterAttribute : ActionFilterAttribute, IActionFilter | |
{ | |
private Stopwatch _stopwatch; | |
private static Logger _logger = LogManager.GetCurrentClassLogger(); | |
public override void OnActionExecuting(ActionExecutingContext filterContext) | |
{ | |
_logger.Info(string.Format("Request ({0}) start at {1}", filterContext.HttpContext.Request.Url.AbsolutePath, DateTime.Now)); | |
_stopwatch = Stopwatch.StartNew(); | |
} | |
public override void OnActionExecuted(ActionExecutedContext filterContext) | |
{ | |
_stopwatch.Stop(); | |
_logger.Info(string.Format("Request ({0}) end at {1} - time {2}", filterContext.HttpContext.Request.Url.AbsolutePath, DateTime.Now, | |
_stopwatch.ElapsedMilliseconds)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment