NLog MVC Integration

By Mike Hanson at April 05, 2012 19:23
Filed Under: .NET, Logging, NLog, MVC, DbContext, Code First, Migrations

Silverlight 2 and ASP.NET MVC showed their heads around the same time and despite all my experience developing with ASP.NET WebForms I jumped in the direction of Silverlight and missed the magic of MVC.  Recently I had the opportunity to work on a little MVC project and really enjoyed the experience.  Since then I have been working on re-writing a personal project, initially I started with MVC 3 but have since moved it up to 4.  I generally use NLog for logging and wanted to integrate it into my MVC project and wanted to use Entity Framework DbContext, Code First and Migrations.  I figured this might be something others would want to do so set about creating a little library to make it easy and fairly painless.  The project lives on github at https://github.com/MikeHanson/NLog.Mvc and includes the following:

 

  • A custom NLog Target that writes log entries to a database using a DbContext instance
  • A custom HandleErrorAttribute that logs exceptions via the NLog Target
  • A Logger utility class that you can instantiate or inject for other components to implement logging throughout your application.
  • I have documented NLog.Mvc within the project wiki at github,, but feel free to ask questions or give me feedback or event request features.  It is working as I need it to in my project, but I haven’t exhaustively tested in other scenarios.

Tag cloud

Previous Articles