我对nlog很新。 我有一个使用nlog的.NET框架控制台应用程序。我希望配置nlog直接将日志写入控制台。我在nlog.config中安装了nlog和nlog.config nuget包,其中包含以下内容
<?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");
在线查看但到目前为止还没有找到任何结果。
答案 0 :(得分:4)
寻找官方教程:link
您需要添加输出规则:
<rules>
<logger name="*" minlevel="Info" writeTo="console" />
</rules>
同时简化您的控制台目标:
<target name="console" xsi:type="Console" />
许多有用的示例都在这里:Most useful NLog configurations
答案 1 :(得分: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 ... -->
答案 2 :(得分:0)
您也可以从代码中找到数字:
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");