11

我对 NLog 很陌生。我有一个使用 NLog 的 .NET 框架控制台应用程序。我希望配置NLog直接将日志写入控制台。我安装了 NLog 和 NLog.Config NuGet 包,在 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");

上网查了一下,到现在什么都没找到。

4

3 回答 3

29

在这里查看官方教程

您需要添加输出规则:

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

还要简化您的控制台目标:

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

这里有很多有用的示例:最有用的 NLog 配置

于 2018-06-15T22:58:53.410 回答
1

您也可以从代码中进行配置:

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");
于 2020-06-19T10:58:34.077 回答
0

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

虽然我发现在另一个窗口中打开 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="..\..\..\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 ... -->

于 2020-05-15T13:36:54.860 回答