I’m using NLog.Web.AspNetCore to makings logs, but Nlog logs only to one all with date .log file (allfile name in nlog.config).
There is my nlog.config file
<?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"
internalLogLevel="Info"
internalLogFile="c:tempinternal-nlog-AspNetCore.txt">
<!-- enable asp.net core layout renderers -->
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<!-- the targets to write to -->
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="request-time"
fileName="(absolute path)Logrequest-time.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
<target xsi:type="File" name="exceptions"
fileName="(absolute path)Lognlog-all-exceptions-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
<target xsi:type="File" name="allfile"
fileName="(absolute path)Lognlog-all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
</targets>
<!-- rules to map from logger name to target -->
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile" />
<logger name="kurs_ASP_dotNET___Restaurant_API.Controllers.*" minlevel="Error" writeTo="exceptions" />
<logger name="kurs_ASP_dotNET___Restaurant_API.Middleware.RequestTimeMiddleware" minlevel="Trace" writeTo="request-time" />
</rules>
</nlog>
And there is my example file which trying logs error.
namespace kurs_ASP_dotNET___Restaurant_API.Services;
public class RestaurantService : IRestaurantService
{
private readonly ILogger<RestaurantService> _logger;
public RestaurantService(ILogger<RestaurantService> logger)
{
_logger = logger;
}
public void Delete(int id)
{
_logger.LogError($"Restaurant with id: {id} DELETE action invoked");
...
}
}
This fragment of code should create "nlog-all-exceptions-${shortdate}.log" file and logs there every usage of .LogError
2
Answers
It seems as though you may need to update your rules for the specific logger; give this a try:
you could make some changes such as logs level and file location for suit your local project.