Search This Blog

Tuesday, July 4, 2017

using nlog from c# code

using NLog;
using NLog.Targets;
using NLog.Config;
using NLog.Win32.Targets;

            // Create configuration object
             LoggingConfiguration config = new LoggingConfiguration();

            // Create targets and add them to the configuration
             ColoredConsoleTarget consoleTarget = new ColoredConsoleTarget();
             config.AddTarget(“console”, consoleTarget);
             FileTarget fileTarget = new FileTarget();
             config.AddTarget(“file”, fileTarget);

            // Set target properties
             consoleTarget.Layout = “${date:format=HH\\:MM\\:ss}: ${message}”;
             // set the file
             fileTarget.FileName = CxUtil.getDataDir() + “\\log.txt”;
             fileTarget.Layout = “${date:format=HH\\:MM\\:ss}: ${message}”;
             // don’t clutter the hard drive
             fileTarget.deleteOldFileOnStartup = true;

            // Define rules
             LoggingRule rule1 = new LoggingRule(“*”, LogLevel.Debug, consoleTarget);
             // Only log to file in non-debug mode
             #if !DEBUG
                 LoggingRule rule2 = new LoggingRule(“*”, LogLevel.Debug, fileTarget);

            // Activate the configuration
             LogManager.Configuration = config;

            // Create logger
             Logger logger = LogManager.GetLogger(“Example”);
             // Example usage
             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”);

1 comment:

endy smith said...

i want to know more about the thigs like that. I really like the information paper assistance uk get essay editor com Don't let me down