skip to Main Content

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


  1. It seems as though you may need to update your rules for the specific logger; give this a try:

    <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" />
            <logger name="kurs_ASP_dotNET___Restaurant_API.Services" minlevel="Error" writeTo="exceptions" />
    
    </rules>
    
    Login or Signup to reply.
  2. you could make some changes such as logs level and file location for suit your local project.

    <targets async="true">
        <!-- Define different file targets. -->
        <target xsi:type="File" name="infoFile"
                fileName="${basedir}/logs/info-${shortdate}.log"
                layout="${longdate}|${level}|${logger}|${message}" />
    
        <target xsi:type="File" name="warnFile"
                fileName="${basedir}/logs/warn-${shortdate}.log"
                layout="${longdate}|${level}|${logger}|${message}" />
    
        <target xsi:type="File" name="errorFile"
                fileName="${basedir}/logs/error-${shortdate}.log"
                layout="${longdate}|${level}|${logger}|${message}" />
    
        <target xsi:type="File" name="allLog"
                fileName="${basedir}/logs/all-log-${shortdate}.log"
                layout="${longdate}|${level}|${logger}|${message}" />
    </targets>
    
    <rules>
        <!-- Define rules to write logs into separate files.-->
        <logger name="*" minlevel="Info" maxlevel="Info" writeTo="infoFile" />
        <logger name="*" minlevel="Warn" maxlevel="Warn" writeTo="warnFile" />
        <logger name="*" minlevel="Error" writeTo="errorFile" />
    
        <!-- write all levels info in a file-->
        <logger name="*" minlevel="Trace" writeTo="allLog" />
    </rules>
    
     
        
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search