.netnlognlog-configuration

Have NLog write to console


I'm pretty new to NLog. I have a .NET framework console application using NLog. I hope to configure NLog to write the log to console directly. I installed NLog and the NLog.Config NuGet package, with the following content in nlog.config:

<?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"
      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="Console"
            name="String"
            layout="Layout"
            footer="Layout"
            header="Layout"
            encoding="Encoding"
    />
  </targets>
</nlog>

Then in C#, the following two lines won't print to the console:

var logger = LogManager.GetCurrentClassLogger();
logger.Info("hello");

Looked online but didn't find anything so far.


Solution

  • Check out the official tutorial here.

    You need to add output rules:

    <rules>
        <logger name="*" minlevel="Info" writeTo="console" />
    </rules>
    

    Also simplify your console target:

    <target name="console" xsi:type="Console" />
    

    Many useful samples are here: Most useful NLog configurations