简体   繁体   中英

How to set debugEnabled/InfoEnabled in nlog.config

I have Logger of NLog package, I want to define trace enable=true in my config logger file.

How can I do it?

My NLog.config file:

   <?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">

  <targets>
    <target name="logfile" xsi:type="File" fileName="file.txt" />
    <target name="logconsole" xsi:type="Console" />
  </targets>

  <rules>
    <logger name="*" maxlevel="Trace" minlevel="Trace" writeTo="logfile" />

  </rules>
</nlog>

You do this by setting the minLevel attribute in your rules section of the NLog.config like so;

<rules>
  <logger name="*" minlevel="Debug" writeTo="CSVFile" />
</rules>

Debug will write out Debug , Info , Warn , Error and Fatal -level log messages. Only Trace is skipped.

EDIT

Conversely, if you wanted to only log Trace , then set the maxLevel like so;

<rules>
  <logger name="*" maxlevel="Trace" writeTo="CSVFile" />
</rules>

To just log a specific intermediate level, use minLevel and maxLevel together like;

<rules>
  <logger name="*" minlevel="Debug" maxlevel="Debug" writeTo="CSVFile" />
</rules>

EDIT2

I tweaked your config file such that the output logs to the console rather than the file;

<?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">

  <targets>
    <target name="logfile" xsi:type="File" fileName="file.txt" />
    <target name="logconsole" xsi:type="Console" />
  </targets>

  <rules>
    <logger name="*" maxlevel="Trace" writeTo="logconsole" />
  </rules>
</nlog>

And when run in a test application where I'm writing out multiple logging levels like so;

_logger.Trace("trace text");
_logger.Debug("debug text");
_logger.Info("info text");
_logger.Warn("warn text");
_logger.Error("error text");
_logger.Fatal("fatal text");

And only the Trace text displays in the console window. One thing of note however, is that the XML in the config file must be correctly formatted. The sample above is working as described in my test application here.

You can create multiple levels of logging by creating multiple logger instaances such as

 <rules>
  <!-- add your logging rules here -->
  <!-- different logger instances for different environments, no levels are mentioned explicitly, can be done if exclusion of some levels is required -->
  <logger name="Log.Dev" minlevel="Debug" writeTo="database" enabled="true" />
  <logger name="Log.Staging" minlevel="Info" writeTo="database" enabled="true" />
  <logger name="Log.Production" minlevel="Error" writeTo="database" enabled="true" />
</rules>

After that while initializing give the name of the logger which you want to initialize

        /// <summary>
    /// The log. 
    /// Getting Logger instance from key name defined in Web.config of main web file. Key name is LoggerInstanceName
    /// On the basis of name , logger instance and defined rules can be switched from one to another.
    /// </summary>
    private static readonly NLog.Logger Log = LogManager.GetLogger(Convert.ToString(ConfigurationManager.AppSettings.Get("LoggerInstance")));

Also do not forget to add key in your app settings

<appSettings>
<!--logger instance name-->
<add key="LoggerInstanceName" value="Log.Dev" />
</appSettings>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM