/// <summary> /// Constructor setting up tray icon, config + status, timer and file system watcher. /// </summary> /// <param name="baseDir">The base directory of the AutoTx service installation.</param> public AutoTxTray(string baseDir) { SetupLogging(); _statusFile = Path.Combine(baseDir, "var", "status.xml"); Log.Info("-----------------------"); Log.Info("{0} initializing...", AppTitle); Log.Info("build: [{0}]", Properties.Resources.BuildDate.Trim()); Log.Info("commit: [{0}]", Properties.Resources.BuildCommit.Trim()); Log.Info("-----------------------"); Log.Debug(" - status file: [{0}]", _statusFile); _notifyIcon.Icon = _tiStopped; _notifyIcon.Visible = true; _notifyIcon.DoubleClick += PickDirectoryForNewTransfer; // this doesn't work properly, the menu will not close etc. so we disable it for now: // _notifyIcon.Click += ShowContextMenu; Log.Trace("Trying to read service config and status files..."); try { _config = ServiceConfig.Deserialize(Path.Combine(baseDir, "conf")); _submitPath = Path.Combine(_config.IncomingPath, Environment.UserName); UpdateStatusInformation(); SetupContextMenu(); var fsw = new FileSystemWatcher { Path = Path.Combine(baseDir, "var"), NotifyFilter = NotifyFilters.LastWrite, Filter = "status.xml", }; fsw.Changed += StatusFileUpdated; fsw.EnableRaisingEvents = true; Log.Info("{0} initialization completed.", AppTitle); } catch (Exception ex) { var msg = "Error during initialization: " + ex.Message; Log.Error(msg); _notifyIcon.ShowBalloonTip(5000, AppTitle, msg, ToolTipIcon.Error); // we cannot terminate the message loop (Application.Run()) while the constructor // is being run as it is not active yet - therefore we set the _status object to // null which will terminate the application during the next "Elapsed" event: _status = null; // suspend the thread for 5s to make sure the balloon tip is shown for a while: System.Threading.Thread.Sleep(5000); } // we need to enable the timer no matter whether the initialization steps above have // succeeded since this is the only way to cleanly exit the application (by checking // the _status in the AppTimerElapsed method): AppTimer.Elapsed += AppTimerElapsed; AppTimer.Enabled = true; Log.Trace("Enabled timer."); }
private static void Main(string[] args) { var logLevel = LogLevel.Info; var logPrefix = ""; var baseDir = AppDomain.CurrentDomain.BaseDirectory; if (args.Length > 0) { baseDir = args[0]; } if (args.Length > 1) { if (args[1] == "debug") { logLevel = LogLevel.Debug; logPrefix = @"${date:format=yyyy-MM-dd HH\:mm\:ss} "; } if (args[1] == "trace") { logLevel = LogLevel.Trace; logPrefix = @"${date:format=yyyy-MM-dd HH\:mm\:ss} (${logger}) "; } } var logConfig = new LoggingConfiguration(); var consoleTarget = new ConsoleTarget { Name = "console", Layout = logPrefix + @"[${level}] ${message}", }; logConfig.AddTarget("console", consoleTarget); var logRuleConsole = new LoggingRule("*", logLevel, consoleTarget); logConfig.LoggingRules.Add(logRuleConsole); LogManager.Configuration = logConfig; const string mark = "----------------------------"; try { Console.WriteLine($"\nTrying to parse configuration files from [{baseDir}]...\n"); _config = ServiceConfig.Deserialize(baseDir); Console.WriteLine($"\n{mark} configuration settings {mark}"); Console.Write(_config.Summary()); Console.WriteLine($"{mark} configuration settings {mark}\n"); } catch (Exception ex) { Console.WriteLine(ex); Environment.ExitCode = -1; } }
/// <summary> /// Load the configuration. /// </summary> private void LoadConfig() { try { _config = ServiceConfig.Deserialize( Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "conf")); } catch (ConfigurationErrorsException ex) { Log.Error("Validating configuration failed: {0}", ex.Message); throw new Exception("Error validating configuration."); } catch (Exception ex) { Log.Error("Loading configuration failed: {0}", ex.Message); // this should terminate the service process: throw new Exception("Error loading configuration."); } SetupFileLogging(_config.LogLevel); }
/// <summary> /// Load the configuration and update the logger setups. /// </summary> private void LoadConfig() { try { _config = ServiceConfig.Deserialize( Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "conf")); } catch (ConfigurationErrorsException ex) { Log.Error("Validating configuration failed: {0}", ex.Message); throw new Exception("Error validating configuration."); } catch (Exception ex) { Log.Error("Loading configuration failed: {0}", ex.Message); // this should terminate the service process: throw new Exception("Error loading configuration."); } // update the file logger with the configured log level: SetupFileLogging(_config.LogLevel); // the mail logger requires the configuration to be present, so we can call it now: SetupMailLogging(); }