E
E
e1s2015-10-27 13:18:06
C++ / C#
e1s, 2015-10-27 13:18:06

NLog is it possible to write to the same file with different styles?

Is it possible when creating a log using NLog to write information to one file, let's say a text file, using a different design for the output (layout) depending on the layer (level). Let's say the design for the Error layer is one, and for the Debug layer it's different.
And accordingly, when called in the application

Logger logger = LogManager.GetCurrentClassLogger();
//здесь бы использовался более подробный шаблон
//с выводом информации об исключении допустим
logger.Error(ex, "message"); 
//здесь использовалась более упрощенное - дата и сообщение
logger.Debug("message")

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Georgy Grigoriev, 2015-11-05
@IamKarlson

Yes, you do not specify minlevel, but levels in the logger
and configure the layout in the target

<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
              layout="${longdate} ${uppercase:${level}} ${message}" />

    <logger name="*" levels="Debug" writeTo="f" />

NLog wiki
Layouts
Layout
renderers
<xs:attribute name="levels" type="NLogLevelList">
      <xs:annotation>
        <xs:documentation>Comma separated list of levels that this rule matches.</xs:documentation>
      </xs:annotation>
    </xs:attribute>

  <xs:simpleType name="NLogLevel">
    <xs:restriction base="xs:string">
      <xs:enumeration value="Off" />
      <xs:enumeration value="Trace" />
      <xs:enumeration value="Debug" />
      <xs:enumeration value="Info" />
      <xs:enumeration value="Warn" />
      <xs:enumeration value="Error" />
      <xs:enumeration value="Fatal" />
    </xs:restriction>
  </xs:simpleType>

Minimal example:
Packages in a project:
<packages>
  <package id="NLog" version="4.2.0" targetFramework="net452" />
  <package id="NLog.Config" version="4.2.0" targetFramework="net452" />
  <package id="NLog.Schema" version="4.0.0" targetFramework="net452" />
</packages>

nlog config:
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log" >
  <targets>
      <target xsi:type="File" name="debug" fileName="${basedir}/logs/${shortdate}.log" layout="DEBUG ${longdate} ${uppercase:${level}} ${message}" />
      <target xsi:type="File" name="info" fileName="${basedir}/logs/${shortdate}.log" layout="INFO ${longdate} ${uppercase:${level}} ${message}" />
      <target xsi:type="File" name="errors" fileName="${basedir}/logs/${shortdate}.log" layout="ERROR ${longdate} ${uppercase:${level}} ${message}" />

  </targets>

  <rules>
      <logger name="*" levels="Debug" writeTo="debug" />
      <logger name="*" levels="Info" writeTo="info" />
      <logger name="*" minlevel="Warn" writeTo="errors" />
  </rules>
</nlog>

Program text:
using System;
using NLog;

namespace ConsoleSandboxApplication
{
    class Program
    {
        private static readonly Logger _logger = LogManager.GetCurrentClassLogger();

        static void Main(string[] args)
        {
            try {
                _logger.Debug("test debug message");
                _logger.Info("some information about program in runtime");
                throw new NotImplementedException("not implemented exception");
            }
            catch (Exception ex) {
                _logger.Error(ex);
            }
        }
    }
}

The log output will look like this in this case:
DEBUG 2015-11-10 09:49:55.7685 DEBUG test debug message
INFO 2015-11-10 09:49:55.8275 INFO some information about program in runtime
ERROR 2015-11-10 09:49:56.7916 ERROR System.NotImplementedException: not implemented exception
   at ConsoleSandboxApplication.Program.Main(String[] args) in S:\projects\other\ConsoleSandboxApplication\ConsoleSandboxApplication\Program.cs:line 20

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question