-
-
Save LSTANCZYK/23993e4f178f08048dd39e99253d0f85 to your computer and use it in GitHub Desktop.
Custom Web API Filter with Log4net logging.
This file contains 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 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