让 NLog 写入控制台

Have NLog write to console

我是 NLog 的新手。我有一个使用 NLog 的 .NET Framework 控制台应用程序。我希望配置 NLog 将日志直接写入控制台。我安装了 NLog 和 NLog.Config NuGet package,在 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>

然后在 C# 中,以下两行将不会打印到控制台:

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

已在线查看,但目前未找到任何内容。

查看 the official tutorial here

您需要添加输出规则:

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

同时简化您的控制台目标:

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

这里有很多有用的示例:Most useful NLog configurations

答案:实现的解决方案是在 Visual Studio 中写入输出调试器 window,因此 NLog 日志将在输出 window 中报告并带有以下 Web.Config 标记。

尽管我发现在另一个 window 中打开 NotePad++ 是更好的解决方案。

<?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="..\..\..\logsTIX_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 ... -->

您也可以通过代码进行配置:

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;

使用:

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