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);
config.LoggingRules.Add(rule1);
// Only log to file in non-debug mode
#if !DEBUG
LoggingRule rule2 = new LoggingRule(“*”, LogLevel.Debug, fileTarget);
config.LoggingRules.Add(rule2);
#endif
// 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”);
No comments:
Post a Comment