/// <summary> /// This EXE is only used for ad-hoc testing of various functionality. It is not to be used for actual builds or MSI files. /// </summary> /// <param name="args"></param> static void Main(string[] args) { // serilog IRuntimeSettings rs = RuntimeSettingsProvider.Instance.GetRuntimeSettings(); IFileManager fm = new LocalFileManager(); IAppLogger logger = new SerilogAppLogger(rs, fm); ISmtpHelper smtp = new SmtpHelper(rs, logger); var subject = "Console"; var body = DateTime.Now.TimeOfDay.ToString(); smtp.Send(subject, body); Console.WriteLine(); Console.WriteLine("Press ENTER to quit"); Console.ReadLine(); }
/// <summary> /// The main entry point for the application. /// </summary> static void Main(string[] args) { IRuntimeSettings rs = RuntimeSettingsProvider.Instance.GetRuntimeSettings(); IFileManager fm = new LocalFileManager(); IAppLogger logger = new SerilogAppLogger(rs, fm); AlerterService targetService; try { targetService = new AlerterService(rs, logger, fm); } catch (Exception ex) { var errorMessage = ex.ToString(); if (ex.InnerException != null) { errorMessage += $" --- Inner Exception = {ex.InnerException}"; } Console.WriteLine($"Unhandled exception when intializing {nameof(targetService)} -- Details: {errorMessage}"); Environment.ExitCode = 999; return; } if (!rs.IsLocalDebugging && args.Length == 0) { // run as windows service var servicesToRun = new ServiceBase[] { targetService }; ServiceBase.Run(servicesToRun); return; } // allowed args const string CommandlineSwitchArg = "CLI"; const string HelpShortSwitch = "H"; const string HelpLongSwitch = "HELP"; var defaultErrorMessage = $"Unable to start. Use switch '{CommandlineSwitchArg}' to run as commandline, or use '{HelpShortSwitch}' or '{HelpLongSwitch}' for help."; if (!rs.IsLocalDebugging && args.Length != 1) { // not in local debugging mode and arg list is missing or greater than 1 --> exit with error logger.Error(defaultErrorMessage); Environment.ExitCode = 1; return; } // grab first arg if it exists var receivedArg = args.Any() ? Regex.Replace(args[0].Trim().ToUpper(), "[^a-zA-Z]", string.Empty) : null; if (rs.IsLocalDebugging && receivedArg == null) { // assume this is running from visual studio and run as CLI RunCommandline(targetService, logger); return; } // decide behavior from arg value switch (receivedArg) { case null: case HelpShortSwitch: case HelpLongSwitch: logger.Info($"You can run from commandline with '{CommandlineSwitchArg}' param, or register and run this as a Windows Service."); break; case CommandlineSwitchArg: RunCommandline(targetService, logger); break; default: logger.Error($"Did not recognize argument '{receivedArg}'. Use switch '{HelpLongSwitch}' for more info."); Environment.ExitCode = 2; break; } }