Nlog logo
.NET, Blog, Software Development, Web Development

NLog with SQL Server and MVC 4

NLog is an awesome open-source logging tool that allows developers to easily and efficiently implement custom logging. Nlog can be configured to log to a number of targets, but on this tutorial we will be looking at logging to the database.

First you need to add NLog to your MVC website. Bring up the Nuget package manager and search for NLog. Add the following packages:

  • NLog
  • NLog Schema for Intellisense
  • NLog configuration
  • NLog for Extended Profile

This will add the necessary dlls and config files for use later. Once the installation is complete, you should see the following file at the root of your application : NLog.config. Double-click to edit it in Visual Studio. Copy and paste the following xml:

So let’s break it down further…

The first part is the main Nlog configuration and we enable custom logging for any errors that may get thrown during the Nlog initialization. It can be handy when chasing down why Nlog is not working. The internal logging is turned on by the throwExceptions property and the internalLogLevel sets the minimum log type to log. Finally the internalLogFile points to the file that the internal errors should be written.

Next we will set up a file logger. This logger will log all logs to a file asynchronously.

The filename will be the be written to the Logs directory and the name will consist of the current date. This way, a new log file will be generated for each new date keeping the file size manageable and the files responsive, especially if your site performs heavy logging. The logger will write the following information to the file:

  • ${longdate} : a datetime stamp
  • ${uppercase:${level}} : the log level
  • ${message}: the custom message you wish to log
  • ${aspnet-user-identity} : the wedsite user. If unauthenticated, then this will be blank.

Note that that this information can only be captured if you have a reference to the NLog for Extended Profile dll.

Next we will create the database logger. Before we continue, we need to make sure that a database exists (sqllite or sqlserver) and that a table called Logs is also present. If you don’t have one, the script to create it is supplied below:

Make sure you run this script against your database! Also, depending on the queries and reports you may generate in the future, you may want to rethink about your keys. In this example I will create a ‘useless’ key for educational purposes. Feel free to edit it as you see fit.  NLog can take it one step further and manage the database and table creation by adding the appropriate commands in the config file, but in the scenario I decide to simplify the overall process and keep the possibility of an error to a minimum.

Now we are ready to create the NLog configuration for the database. The important properties are the connectionString and the commandText. Finally, the parameters are used to map between the NLog values and the insert command values.

Before any logging takes place, we need to tell NLog which log levels to capture. You could either create a rule for each target or you could roll all of them into one as I will demonstrate below:

This rule simple instructs NLog to log all the entries at Debug level and above to both targets. NLog supports the following Log Levels:

  • Trace – very detailed logs, which may include high-volume information such as protocol payloads. This log level is typically only enabled during development
  • Debug – debugging information, less detailed than trace, typically not enabled in production environment.
  • Info – information messages, which are normally enabled in production environment
  • Warn – warning messages, typically for non-critical issues, which can be recovered or which are temporary failures
  • Error – error messages
  • Fatal – very serious errors

This are all the configuration changes you need to do to setup NLog. The final step is to create a logger in our application and start logging.

For the internal logging to work you need to open the Global.asax and add the following two lines to the Application_Start() to indicate where the InternalLogger should log the exceptions

Finally, you can instantiate a logger and start logging in your controllers. I have added some basic log statements in my Home controller:

To create an instance of a logger, just call the LogManager.GetCurrentClassLogger(); To log a message, call the logger.Info(“you custom message goes here”);

More information and examples how to use NLog can be found here

Happy coding….

Standard