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. Create a new ASP.NET Core Web API Application and install NLog.Web.AspNetCore via nuget.
Install-Package NLog.Web.AspNetCore
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
throwConfigExceptions="true">
<!-- the targets to write to -->
<targets>
<target xsi:type="File"
name="archive"
archiveEvery="Day"
archiveFileName = "nlogdemo-{########}.log"
archiveNumbering = "Date"
archiveDateFormat = "yyyyMMdd"
maxArchiveFiles = "4"
fileName="nlogdemo.log"
layout="${longdate}|${level:uppercase=true}|${logger}|${message}" />
</targets>
<!-- rules to map from logger name to target -->
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Warn" writeTo="archive" />
</rules>
</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.
private readonly ILogger _logger;
public ValuesController(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<ValuesController>();
}
// GET api/values
[HttpGet]
public IEnumerable<string> Get()
{
_logger.LogDebug("debug");
_logger.LogError("error");
_logger.LogTrace("trace");
_logger.LogInformation("info");
_logger.LogWarning("warn");
_logger.LogCritical("critical");
return new string[] { "value1", "value2" };
}
Best ASP.NET Core 2.2.1 Hosting Recommendation
One of the most important things when choosing a good ASP.NET Core 2.2.1 hosting is the feature and reliability. HostForLIFE is the leading provider of Windows hosting and affordable ASP.NET Core 2.2.1 , their servers are optimized for PHP web applications such as the latest ASP.NET Core 2.2.1 version. The performance and the uptime of the ASP.NET Core 2.2.1 hosting service are excellent and the features of the web hosting plan are even greater than what many hosting providers ask you to pay for. At HostForLIFEASP.NET, customers can also experience fast ASP.NET Core 2.2.1 hosting. The company invested a lot of money to ensure the best and fastest performance of the datacenters, servers, network and other facilities. Its datacenters are equipped with the top equipments like cooling system, fire detection, high speed Internet connection, and so on. That is why HostForLIFEASP.NET guarantees 99.9% uptime for ASP.NET Core 2.2.1. And the engineers do regular maintenance and monitoring works to assure its ASP.NET Core 2.2.1 hosting are security and always up.