Configure NLog for ASP.NET MVC in 3 Steps

Configure NLog for ASP.NET MVC in 3 Steps, nlog tutorial, nlog config, nlog, nlog.config, nlog configuration, nlog levels, nlog.config example, nlog log levels, nlog web.config, log levels nlog, how to use nlog, asp.net mvc nlog, c# nlog, nlog config example, asp net core nlog, nlog example, nlog.web, nlog xml, nlog asp.net, nlog c#, nlog machinename, asp.net nlog, nlog asp.net mvc, nlog c# example, nlog trace, configure nlog, nlog configuration c#, what is nlog, nlog layout, nlog mvc, nlog config example c#, nlog configuration example

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 Config)

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: Configuring NLog for C# (NLog web.config)

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 Tutorial)

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");
  [...]
}

Post a Comment

Previous Post Next Post