12

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.

Luke Girvin
  • 12,913
  • 8
  • 60
  • 81
checai
  • 720
  • 3
  • 9
  • 15

3 Answers3

32

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

Pang
  • 9,073
  • 146
  • 84
  • 117
Alexey.Petriashev
  • 1,308
  • 11
  • 19
  • How do you view the console when working in Visual Studio on a Web application. Which file menue > view > menu window? I have Output window open in Visual Studio > Show output from Debug, but nothing is logging. I am logging to a text file which is working fine. Thank you. – Moojjoo May 04 '20 at 18:24
  • launch it as console (select in combobox near Start) – Alexey.Petriashev May 14 '20 at 10:55
2

You can configure from code as well:

var config = new NLog.Config.LoggingConfiguration();

// Targets where to log to: Console
var logconsole = new NLog.Targets.ConsoleTarget("logconsole");

// Rules for mapping loggers to targets
config.AddRule(LogLevel.Info, LogLevel.Fatal, logconsole);

// Apply config
NLog.LogManager.Configuration = config;

Use:

var logger = NLog.LogManager.GetCurrentClassLogger();
logger.Info("hello");
Pang
  • 9,073
  • 146
  • 84
  • 117
Mariusz Jamro
  • 29,116
  • 24
  • 107
  • 151
0

Answer: Solution implemented to write to the output debugger window in Visual Studio so the NLog logs will report in the Output window with the following Web.Config markup.

Althought I have found that having NotePad++ Open in the other window is a better solution.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
  </configSections>
<nlog xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd" 
        autoReload="true" 
        throwExceptions="true"         
        xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <extensions>
      <add assembly="WellsFargo.Diagnostics.Integration" />
    </extensions>
    <!-- optional, add some variables
  https://github.com/nlog/NLog/wiki/Configuration-file#variables
  -->
    <!--<variable name="myvar" value="myvalue" />-->
    <!--
  See https://github.com/nlog/nlog/wiki/Configuration-file
  for information on customizing logging rules and outputs.
   -->
    <targets async="true">
      <!--
    add your targets here
    See https://github.com/nlog/NLog/wiki/Targets for possible targets.
    See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
    -->
      <target name="Mail" xsi:type="Mail" html="true" subject="Error Received" body="${longdate} ${uppercase:${level}} | ${callsite} | ${message} | ${newline}" to="robert.b.dannellyjr@wellsfargo.com" from="1TIX_NLog@wellsFargo.com" smtpServer="SMTP.AZURE.WELLSFARGO.NET" smtpPort="25"/>
      <target xsi:type="File" name="logfile" fileName="..\..\..\logs\1TIX_NLog\AppNLog.log"
              archiveEvery="Day"
              archiveFileName="..\..\..\logs\App_NLog\AppNLog.{#}.log"
              archiveNumbering="DateAndSequence"
              archiveDateFormat="yyyy-MM-dd"
              archiveAboveSize="104857600"
              maxArchiveFiles="14" />
      <target xsi:type="Debugger" name="debugger" layout="${longdate} ${uppercase:${level}}|${callsite}|${message}" />
      <target xsi:type="NLogAppender" name="NLogAppender" layout="${longdate} ${uppercase:${level}}|${callsite}|${message}|${newline}" />
      <!--<target xsi:type="Console" name="f" layout="${longdate} ${uppercase:${level}} | ${callsite} | ${message} | ${newline}" />-->
    </targets>
    <rules>
      <!-- add your logging rules here -->
      <logger name="*" levels="Error,Fatal" writeTo="Mail" />
      <logger name="*" minlevel="Debug" writeTo="logfile" />
<!-- Writes NLog to debugger window in Visual Studio -->
      <logger name="*" minlevel="Trace" writeTo="debugger" />
<!-- Writes NLogs to Splunk -->
      <logger name="*" minlevel="Trace" writeTo="NLogAppender" /> 
      <!--
    Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace)  to "f"
    <logger name="*" minlevel="Debug" writeTo="f" />  -->
    </rules>
  </nlog>
<connectionStrings>
<!-- Code Omitted ... -->

Moojjoo
  • 681
  • 1
  • 14
  • 31