Steps to archive log files using NLog with ASP.NET Core

InfoPath alternatives for form designing SharePoint

Tags: nlog in core, nlog in core 2.0, nlog in asp net core 2.0 tutorial, core 2.0 nlog, nlog core 2, Use NLog in ASP.NET Core 2.0, ASP.NET Core NLog

In this article, we will learn how to archive log files using NLog with ASP.NET Core .Most of the time, we will store logs in files, and we should archive them so that we can manage them easier. Let’s take a look at how to use NLog to archive log files.

Steps To Be Followed:

Step-1: New Application

Create a new ASP.NET Core Web API Application and install NLog.Web.AspNetCore via nuget.

Install-Package NLog.Web.AspNetCore

Step-2: nlog.config

Create a nlog.config file, and enable copy to bin folder.

Read: How to set up session state in our ASP.NET Core and MVC Core Web applications?

We just archive the log files via this configuration file.

<?xml version=”1.0″ encoding=”utf-8″ ?>

<nlog xmlns=””




<!– the targets to write to –>


<target xsi:type=”File”



archiveFileName = “nlogdemo-{########}.log”

archiveNumbering = “Date”

archiveDateFormat = “yyyyMMdd”

maxArchiveFiles = “4”


layout=”${longdate}|${level:uppercase=true}|${logger}|${message}” />


<!– rules to map from logger name to target –>


<!–All logs, including from Microsoft–>

<logger name=”*” minlevel=”Warn” writeTo=”archive” />



Just pay attention to the options that contains archive.

The above configuration means that we have a main log file named nlogdemo.log which stores today’s log.

It will archive logs daily and the file name of the archive log file will be formatted like nlogdemo-20180505.log.

And the max number of archived log files is 4 which means it will keep only the newest 4 files.

Step-3: program.cs

Update program.cs so that we can enable NLog.

namespace NLogDemo


using Microsoft.AspNetCore;

using Microsoft.AspNetCore.Hosting;

using NLog.Web;

public class Program


public static void Main(string[] args)




public static IWebHost BuildWebHost(string[] args) =>








Write some logs in controller.

private readonly ILogger _logger;

public ValuesController(ILoggerFactory loggerFactory)


_logger = loggerFactory.CreateLogger<ValuesController>();


// GET api/values


public IEnumerable<string> Get()








return new string[] { “value1”, “value2” };


Now, change the date of computer then we can see log file with updated date.

– Here we discussed about Basic configuration to archive log files using NLog.

You May Also like the Following SharePoint Online Tutorials:

About Satyaprakash Samantaray

MVP Award Winner | Community Author & Contributor | Software Developer | Most Valuable Blogger(MVB)

View all posts by Satyaprakash Samantaray →

Leave a Reply