static void Main(string[] args) { MessageBoxTarget target = new MessageBoxTarget(); target.Layout = "${longdate}: ${message}"; target.Caption = "${level} message"; NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug); Logger logger = LogManager.GetLogger("Example"); logger.Debug("log message"); }
internal static void InitCoreLoggers() { if (LogManager.Configuration == null) { LogManager.Configuration = new LoggingConfiguration(); } else { // User provided custom NLog config, but we still need to define our own logger IsUsingCustomConfiguration = true; if (LogManager.Configuration.AllTargets.Any(target => target is MessageBoxTarget)) { return; } } MessageBoxTarget messageBoxTarget = new MessageBoxTarget { Name = "MessageBox", Layout = GeneralLayout }; LogManager.Configuration.AddTarget(messageBoxTarget); LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Fatal, messageBoxTarget)); LogManager.ReconfigExistingLoggers(); }
static void Main(string[] args) { ExitEvent = new ManualResetEvent(false); MigrateSettingsDirectory(); try { if (!Directory.Exists(AppConfigDirectory)) { IsFirstRun = true; Directory.CreateDirectory(AppConfigDirectory); } Console.WriteLine("App config/log directory: " + AppConfigDirectory); } catch (Exception ex) { MessageBox.Show("Could not create settings directory. " + ex.Message); Application.Exit(); return; } var logConfig = new LoggingConfiguration(); var logFile = new FileTarget(); logConfig.AddTarget("file", logFile); logFile.Layout = "${longdate} ${level} ${message} \n ${exception:format=ToString}\n"; logFile.FileName = Path.Combine(AppConfigDirectory, "log.txt"); logFile.ArchiveFileName = "log.{#####}.txt"; logFile.ArchiveAboveSize = 500000; logFile.MaxArchiveFiles = 1; logFile.KeepFileOpen = false; logFile.ArchiveNumbering = ArchiveNumberingMode.DateAndSequence; var logFileRule = new LoggingRule("*", LogLevel.Debug, logFile); logConfig.LoggingRules.Add(logFileRule); if (Program.IsWindows) { #if !__MonoCS__ var logAlert = new MessageBoxTarget(); logConfig.AddTarget("alert", logAlert); logAlert.Layout = "${message}"; logAlert.Caption = "Alert"; var logAlertRule = new LoggingRule("*", LogLevel.Fatal, logAlert); logConfig.LoggingRules.Add(logAlertRule); #endif } var logConsole = new ConsoleTarget(); logConfig.AddTarget("console", logConsole); logConsole.Layout = "${longdate} ${level} ${message} ${exception:format=ToString}"; var logConsoleRule = new LoggingRule("*", LogLevel.Debug, logConsole); logConfig.LoggingRules.Add(logConsoleRule); LogManager.Configuration = logConfig; LoggerInstance = LogManager.GetCurrentClassLogger(); ReadSettingsFile(); var serverTask = Task.Run(async () => { ServerInstance = new Server(); await ServerInstance.Start(); }); try { if (Program.IsWindows) { #if !__MonoCS__ Application.Run(new Main()); #endif } } catch (Exception) { } Console.WriteLine("Running in headless mode."); Task.WaitAll(serverTask); Console.WriteLine("Server thread exit"); }