It is important to have logging enabled in any production site which will be used by the technical team to debug the issues or monitor the health of the site. We chose BitFactory logging (Now the author is calling as Termite) to implement the logging in WCF Services, Business Layer & ASP.NET MVC Sites.
Why did we choose BitFactory?
- It is very easy to implement.
- Absolutely working with complex categories (we have 43 projects in a solution with more than 25 categories configured for logging)
- No issue with Multithreading or Filelocking issue.
I have used Nlog, Log4net, MS Enterprise library & Elmah in various projects, somehow my choice at this time is BitFactory. Other frameworks are good on their own flavor and i am not against any frameworks whether it is commercial or open source.
In ASP.NET MVC we have to use HandleError attribute to redirect to the customized error page and i would like to implement the logging using the same attribute. Thanks to Mark Seemann for the poor man’s Dependency Injection for the logging. I used the same snippet but tried to integrate BitFactory logging.
ErrorHandlingActionInvoker.cs
public class ErrorHandlingActionInvoker : ControllerActionInvoker
{
private readonly IExceptionFilter filter;
public ErrorHandlingActionInvoker(IExceptionFilter filter)
{
if (filter == null)
{
throw new ArgumentNullException("filter");
}
this.filter = filter;
}
protected override FilterInfo GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
{
var filterInfo =base.GetFilters(controllerContext, actionDescriptor);
filterInfo.ExceptionFilters.Add(this.filter);
return filterInfo;
}
}
ErrorHandlingControllerFactory.cs
public class ErrorHandlingControllerFactory : DefaultControllerFactory
{
public override IController CreateController(RequestContext requestContext,string controllerName)
{
var controller =base.CreateController(requestContext,controllerName);
var c = controller as Controller;
if (c != null)
{
c.ActionInvoker = new ErrorHandlingActionInvoker(new LoggingExceptionFilter(new HandleErrorAttribute()));
}
return controller;
}
}
LoggingExceptionFilter.cs
public class LoggingExceptionFilter : IExceptionFilter
{
private readonly IExceptionFilter filter;
private readonly ConfigLogger logWriter = ConfigLogger.Instance;
public LoggingExceptionFilter(IExceptionFilter filter)
{
if (filter == null)
{
throw new ArgumentNullException("filter");
}
if (logWriter == null)
{
throw new ArgumentNullException("logWriter");
}
this.filter = filter;
}
public void OnException(ExceptionContext filterContext)
{
this.logWriter.LogError(filterContext.Exception);
this.filter.OnException(filterContext);
}
}
Web.Config
<BitFactory.Logging name="global" xmlns="http://BitFactory.Logging">
<rollingDateFileLoggers>
<rollingDateFileLogger isEnabled="true" name="FrontEndLogger" formattedFileName="C:\Logs\services_{timestamp:yyyyMMdd}.log"/>
</rollingDateFileLoggers>
</BitFactory.Logging>
Add this line in Global.asax.cs
ControllerBuilder.Current.SetControllerFactory(new ErrorHandlingControllerFactory());
Happy Coding!