Nlog class library usage exploration-programming Configuration

Source: Internet
Author: User

It is configured in programming mode. This is in my project and I don't know why programming is used. Is the direct configuration not very good? It is estimated that he has not studied. The preceding steps and Code are as follows:

  1. Create a loggingconfiguration object to save configuration information
  2. Create at least one output target object
  3. Set the attributes of the output target object
  4. Set the loggingrule object and add it to the loggingrules set of the loggingconfiguration object
  5. Enable this loggingconfiguration object (set logmanager. configuration to this loggingconfiguration object)
using NLog; using NLog.Targets; using NLog.Config; using NLog.Win32.Targets;  class Example {     static void Main(string[] args)     {         // Step 1. Create configuration object                  LoggingConfiguration config = new LoggingConfiguration();                  // Step 2. Create targets and add them to the configuration          ColoredConsoleTarget consoleTarget = new ColoredConsoleTarget();         config.AddTarget("console", consoleTarget);                  FileTarget fileTarget = new FileTarget();         config.AddTarget("file", fileTarget);                  // Step 3. Set target properties                  consoleTarget.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}";         fileTarget.FileName = "${basedir}/file.txt";         fileTarget.Layout = "${message}";                  // Step 4. Define rules                  LoggingRule rule1 = new LoggingRule("*", LogLevel.Debug, consoleTarget);         config.LoggingRules.Add(rule1);          LoggingRule rule2 = new LoggingRule("*", LogLevel.Debug, fileTarget);         config.LoggingRules.Add(rule2);                  // Step 5. Activate the configuration          LogManager.Configuration = config;                  // Example usage          Logger logger = LogManager.GetLogger("Example");         logger.Trace("trace log message");         logger.Debug("debug log message");         logger.Info("info log message");         logger.Warn("warn log message");         logger.Error("error log message");         logger.Fatal("fatal log message");     } }

Nlog class library usage exploration-programming Configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.