Nlog: Error method also write log to Info file

Created on 23 Nov 2018  ·  2Comments  ·  Source: NLog/NLog

NLog version:4.5.10

Platform: .Net 4.5
Current NLog config (xml or C#, if relevant)

<?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">

  <targets>
    <!--Infoasync="true"-->
    <target name="infofileAsync" xsi:type="AsyncWrapper" overflowAction="Grow" 
            queueLimit="10000" batchSize="200" timeToSleepBetweenBatches="50">
    <target name="infofile"
      xsi:type="File"
      fileName="${basedir}/NLog/InfoLogs/${shortdate}_log.txt"
      layout="[${longdate}][${level:uppercase=true}][${logger}]${message}${exception}"
      archiveFileName="${basedir}/archives/InfoLogs/log.${shortdate}.{#####}.txt"
      archiveAboveSize="10485760"
            archiveNumbering="Sequence"
      maxArchiveFiles="200"
            concurrentWrites="true"
      keepFileOpen="false"/>
    </target>

    <!--Error-->
    <target name="errorfileAsync" xsi:type="AsyncWrapper" overflowAction="Grow"
          queueLimit="10000" batchSize="200" timeToSleepBetweenBatches="50">
      <target name="errorfile"
        xsi:type="File"
        fileName="${basedir}/NLog/ErrorLogs/${shortdate}_log.txt"
        layout="[${longdate}][${level:uppercase=true}][${logger}]${message}${exception}"
        archiveFileName="${basedir}/archives/ErrorLogs/log.${shortdate}.{#####}.txt"
        archiveAboveSize="10485760"
              archiveNumbering="Sequence"
        maxArchiveFiles="400"
              concurrentWrites="true"
        keepFileOpen="false"/>
    </target>
  </targets>
  <rules>
    <logger name="*" minlevel="Info" writeTo="infofileAsync"/>
    <logger name="*" minlevel="Error" writeTo="errorfileAsync"/>
  </rules>
</nlog>

my code is :+1:

```c#
try
{
throw new Exception("ddd");
}
catch(Exception ex)
{
var logger = LogManager.GetCurrentClassLogger();
logger.Error(string.Format("This is {0} cycles current thread {1}",
i, Thread.CurrentThread.ManagedThreadId));
}

when write error information they write to infofileAsync : 

[2018-11-23 19:32:15.6210][INFO][TestLogPreformance.NLogTest]This is 0 cycles current thread 1
[2018-11-23 19:32:15.6320][ERROR][TestLogPreformance.NLogTestOld]This is 0 cycles current thread 1ddd

```

My expectation is that the wrong log is only written in the wrong file and not written to the information log repeatedly.

Thanks!

question

Most helpful comment

Thanks,it work

All 2 comments

@SimenCTan You might want to read about logging rules (controls what targets a named logger-object reaches)

https://github.com/NLog/NLog/wiki/Tutorial#configure-nlog-targets-for-output
https://github.com/NLog/NLog/wiki/Configuration-file#rules

You can consider changing your logging rules to this (If not wanting errors to reach your InfoLogs):

  <rules>
    <logger name="*" minlevel="Error" writeTo="errorfileAsync" final="true" />
    <logger name="*" minlevel="Info" writeTo="infofileAsync"/>
  </rules>

Alternative you can do this:

  <rules>
    <logger name="*" level="Info" writeTo="infofileAsync"/>
    <logger name="*" level="Warn" writeTo="infofileAsync"/>
    <logger name="*" level="Error" writeTo="errorfileAsync" />
    <logger name="*" level="Fatal" writeTo="errorfileAsync" />
  </rules>

Thanks,it work

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vasumsit picture vasumsit  ·  3Comments

smeegoan picture smeegoan  ·  3Comments

npandrei picture npandrei  ·  3Comments

carkov1990 picture carkov1990  ·  3Comments

BobSeu picture BobSeu  ·  3Comments