Configure NLog for ASP.NET MVC in 3 Steps

Error Logging is one of the key attributes when developing ASP.NET Web application. NLog is a great open-source logging tool that allows developers to easily and efficiently implement logging framework. In this post I will show the steps needed to configure and use NLog in your MVC web application

Step 1: Installing NLog

NLog can be downloaded from NuGet

You can use the GUI or the following command in the Package Manager Console:

PM> Install-Package NLog -Version 4.4.1

That's it, you can now compile and run your application and you will be able to use NLog.

Step 2: Configuration

In your web.config file add the following section under <configSections>

<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>

Then add the following section under <configuration>

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="default" xsi:type="File"
fileName="logs/app-log.txt"
archiveFileName="logs/archives/app-log.{#}.txt"
archiveEvery="Day"
archiveNumbering="Rolling"
maxArchiveFiles="7" />
</targets>
<rules>
<logger name="*" writeTo="default" />
</rules>
</nlog>

The final web.config should look like the following:

<?xml version="1.0"?>
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="default" xsi:type="File"
fileName="logs/app-log.txt"
archiveFileName="logs/archives/app-log.{#}.txt"
archiveEvery="Day"
archiveNumbering="Rolling"
maxArchiveFiles="7" />
</targets>
<rules>
<logger name="*" writeTo="default" />
</rules>
</nlog>
</configuration>

Note that I'm using relative path to store the log files. You can use absolute path if you need.

Step3: Writing Log Messages

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 - most of the time these are Exceptions
  • Fatal - very serious errors!

You must use LogManager to create Logger instances, in your controller use the following sample code:

using NLog;

private
static Logger logger = LogManager.GetCurrentClassLogger(); [...] public ActionResult Index() { logger.Trace("Sample trace message");
logger.Debug("Sample debug message");
logger.Info("Sample informational message");
logger.Warn("Sample warning message");
logger.Error("Sample error message");
logger.Fatal("Sample fatal error message");

// alternatively you can call the Log() method
// and pass log level as the parameter.
logger.Log(LogLevel.Info, "Sample informational message");
[...] }

YOU MAY ALSO LIKE...

Zender helps you automate marketing emails throughout the customer journey using a powerful API. Create and Send html based emails, Sign Up Confirmation Emails, Change Password Emails, Campaigns, Advertising and Media News Emails

Check out the API http://zender.sharptag.com and let us know how can we support you.

6 Comments

Add a Comment

As it will appear on the website

Not displayed

Your website