Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LSTANCZYK/23993e4f178f08048dd39e99253d0f85 to your computer and use it in GitHub Desktop.
Save LSTANCZYK/23993e4f178f08048dd39e99253d0f85 to your computer and use it in GitHub Desktop.
Custom Web API Filter with Log4net logging.
public class LogActionWebApiFilter : ActionFilterAttribute
{
/// <summary>
/// Instance of the Log4Net log.
/// </summary>
[Dependency] //Part 1
public ILog Log { get; set; }
//This function will execute before the web api controller
//Part 2
public override void OnActionExecuting(HttpActionContext actionContext)
{
//This is where you will add any custom logging code
//that will execute before your method runs.
Log.DebugFormat(string.Format("Request {0} {1}"
, actionContext.Request.Method.ToString()
, actionContext.Request.RequestUri.ToString());
}
//This function will execute after the web api controller
//Part 3
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
//This is where you will add any custom logging code that will
//execute after your method runs.
Log.DebugFormat(string.Format("{0} Response Code: {1}"
, actionExecutedContext.Request.RequestUri.ToString()
, actionExecutedContext.Response.StatusCode.ToString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment