Hendrik Swanepoel

Asp.Net MVC logging filter

On my current project we are making extensive use of ASP.Net MVC and LINQ, which is a nice change since I've been working on mainly WCF and WF based projects for the last few years.
We immediately identified that it would be worthwhile to create a filter which does logging for us, and this is what I came up with.
   1:  using System.Reflection;
   2:  using System.Text;
   3:  using System.Web.Mvc;
   4:  using Axis.CRD.Logic;
   5:  using log4net;
   6:   
   7:  namespace Hendrik
   8:  {
   9:      public class LoggingFilter : ActionFilterAttribute
  10:      {
  11:          private static readonly ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  12:   
  13:          public override void OnActionExecuted(FilterExecutedContext filterContext)
  14:          {
  15:              if (filterContext.Exception != null)
  16:                  return;
  17:   
  18:              var logMessage = new StringBuilder();
  19:              logMessage.AppendFormat("Finished call to method {0} on controller {1}",
  20:                                      filterContext.ActionMethod.Name, filterContext.Controller);
  21:              var parameters = filterContext.ActionMethod.GetParameters();
  22:              Append(logMessage, filterContext, parameters);
  23:   
  24:              logger.Debug(logMessage);
  25:          }
  26:   
  27:          public override void OnActionExecuting(FilterExecutingContext filterContext)
  28:          {
  29:              var logMessage = new StringBuilder();
  30:              logMessage.AppendFormat("Starting call to method {0} on controller {1}",
  31:                                      filterContext.ActionMethod.Name, filterContext.Controller);
  32:              var parameters = filterContext.ActionMethod.GetParameters();
  33:              Append(logMessage, filterContext, parameters);
  34:              logger.Debug(logMessage);
  35:          }
  36:   
  37:   
  38:          public static void Append(StringBuilder logMessage, FilterContext filterContext, ParameterInfo[] parameters)
  39:          {
  40:              if (parameters.Length > 0)
  41:              {
  42:                  logMessage.AppendLine().Append("Parameters:");
  43:              }
  44:              foreach (var parameter in filterContext.ActionMethod.GetParameters())
  45:              {
  46:                  var passedValue = (filterContext.RouteData.Values.ContainsKey(parameter.Name)) ?
  47:                      filterContext.RouteData.Values[parameter.Name] : filterContext.HttpContext.Request[parameter.Name];
  48:                  passedValue = passedValue ?? filterContext.HttpContext.Request[parameter.Name];
  49:                  logMessage.AppendLine().AppendFormat("*{0} : {1}", parameter.Name, passedValue);
  50:              }
  51:          }
  52:      }
  53:  }

Comments

Ernst Kuschke said:

Very cool. Are you guys running a URL re-writer ISAPI filter on IIS? If so, which one?

# June 11, 2008 9:26 PM

Sgro said:

He clearly stated that this is for MVC (well being an actionfilter it couldn't be for anything else) so explain me why would he need url rewriting...

# June 24, 2008 10:18 AM

hendrik said:

Sorry for the late reply Ernst. No, at the moment we're not doing any url rewriting. It's an intranet site, and developed in an agile manner. Thus far nobody has complained about the .mvc in the urls!

# July 19, 2008 4:42 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: