How To Archive Log Files Using NLog With ASP.NET Core

Introduction

In this article, you 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.

Step 1

Create a new ASP.NET Core Web API Application and install NLog.Web.AspNetCore via nuget.
  1. Install-Package NLog.Web.AspNetCore  
Step 2 

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

We just archive the log files via this configuration file.
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"  
  3.       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.       autoReload="true"  
  5.       throwConfigExceptions="true">  
  6.   
  7.   <!-- the targets to write to -->  
  8.   <targets>  
  9.       
  10.     <target xsi:type="File"   
  11.         name="archive"   
  12.         archiveEvery="Day"  
  13.         archiveFileName = "nlogdemo-{########}.log"  
  14.         archiveNumbering = "Date"  
  15.         archiveDateFormat = "yyyyMMdd"  
  16.         maxArchiveFiles = "4"  
  17.         fileName="nlogdemo.log"  
  18.         layout="${longdate}|${level:uppercase=true}|${logger}|${message}" />   
  19.                       
  20.     </targets>  
  21.   
  22.   <!-- rules to map from logger name to target -->  
  23.   <rules>  
  24.     <!--All logs, including from Microsoft-->  
  25.     <logger name="*" minlevel="Warn" writeTo="archive" />  
  26.   </rules>  
  27. </nlog>  

Just pay attention to the opions that contains archive.

The above configuration means that we hava 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

Update program.cs so that we can enable NLog. 
  1. namespace NLogDemo  
  2. {  
  3.     using Microsoft.AspNetCore;  
  4.     using Microsoft.AspNetCore.Hosting;  
  5.     using NLog.Web;  
  6.   
  7.     public class Program  
  8.     {  
  9.         public static void Main(string[] args)  
  10.         {  
  11.             BuildWebHost(args).Run();  
  12.         }  
  13.   
  14.         public static IWebHost BuildWebHost(string[] args) =>  
  15.             WebHost.CreateDefaultBuilder(args)  
  16.                 .UseStartup<Startup>()  
  17.                 .UseNLog()  
  18.                 .Build();  
  19.     }  
  20. }  
Step 4

Write some logs in controller. 
  1. private readonly ILogger _logger;  
  2.   
  3. public ValuesController(ILoggerFactory loggerFactory)  
  4. {  
  5.     _logger = loggerFactory.CreateLogger<ValuesController>();  
  6. }  
  7.   
  8. // GET api/values  
  9. [HttpGet]  
  10. public IEnumerable<string> Get()  
  11. {  
  12.     _logger.LogDebug("debug");  
  13.     _logger.LogError("error");  
  14.     _logger.LogTrace("trace");  
  15.     _logger.LogInformation("info");  
  16.     _logger.LogWarning("warn");  
  17.     _logger.LogCritical("critical");  
  18.   
  19.     return new string[] { "value1""value2" };  
  20. }  
Now, change the date of computer to see the results.

 
 
 
Source Code
Summary

This article introduced the basic configuration to acrchive log files using NLog.
 
Hope this can help you.