public int Main(string[] arguments) { int result = ExitCodeNoProgramExecuted; try { Version = GetVersion(); LicenseLink = $"https://raw.githubusercontent.com/anionDev/Epew/v{Version}/License.txt"; _SentenceBuilder = SentenceBuilder.Create(); if (arguments == null) { throw Utilities.CreateNullReferenceExceptionDueToParameter(nameof(arguments)); } string argumentsAsString = String.Join(' ', arguments); _Log = GRYLog.Create(); string workingDirectory = Directory.GetCurrentDirectory(); try { if (arguments.Length == 0) { _Log.Log($"{ProgramName} v{Version}"); _Log.Log($"Run '{ProgramName} --help' to get help about the usage."); } else { ParserResult <EpewOptions> parserResult = new Parser(settings => settings.CaseInsensitiveEnumValues = true).ParseArguments <EpewOptions>(arguments); if (IsHelpCommand(arguments)) { WriteHelp(parserResult); } else { parserResult.WithParsed(options => { result = HandleSuccessfullyParsedArguments(options); }) .WithNotParsed(errors => { HandleParsingErrors(argumentsAsString, errors); }); return(result); } } } catch (Exception exception) { _Log.Log($"Fatal error occurred while processing argument '{workingDirectory}> epew {argumentsAsString}", exception); } } catch (Exception exception) { System.Console.Error.WriteLine($"Fatal error occurred", exception.ToString()); result = ExitCodeFatalErroroccurred; } return(result); }
public void TestLogFileWithConfigurationchangeOnRuntime() { string configurationFile = "log.configuration"; string logFile1 = "log1.log"; string logFile2 = "log2.log"; Core.Miscellaneous.Utilities.EnsureFileDoesNotExist(logFile1); Core.Miscellaneous.Utilities.EnsureFileDoesNotExist(logFile2); Core.Miscellaneous.Utilities.EnsureFileDoesNotExist(configurationFile); GRYLogConfiguration configuration = new() { ConfigurationFile = configurationFile }; configuration.ResetToDefaultValues(logFile1); configuration.SetFormat(GRYLogLogFormat.OnlyMessage); GRYLogConfiguration.SaveConfiguration(configurationFile, configuration); UTF8Encoding encoding = new(false); try { using GRYLog logObject = GRYLog.CreateByConfigurationFile(configurationFile); logObject.Log("test1", LogLevel.Information); //will be logged logObject.Log("test2", LogLevel.Debug); //will not be logged because 'debug' is not contained in LogLevels by default Assert.AreEqual("test1", File.ReadAllText(logFile1, encoding)); GRYLogConfiguration reloadedConfiguration = GRYLogConfiguration.LoadConfiguration(configurationFile); reloadedConfiguration.GetLogTarget <LogFile>().LogLevels.Add(LogLevel.Debug); GRYLogConfiguration.SaveConfiguration(configurationFile, reloadedConfiguration); System.Threading.Thread.Sleep(1000); //wait until config is reloaded logObject.Log("test3", LogLevel.Debug); // will now be logged Assert.AreEqual("test1" + Environment.NewLine + "test3", File.ReadAllText(logFile1, encoding)); reloadedConfiguration = GRYLogConfiguration.LoadConfiguration(configurationFile); reloadedConfiguration.SetLogFile(logFile2); GRYLogConfiguration.SaveConfiguration(configurationFile, reloadedConfiguration); System.Threading.Thread.Sleep(1000); //wait until config is reloaded logObject.Log("test4", LogLevel.Debug); // will be logged Assert.AreEqual("test1" + Environment.NewLine + "test3", File.ReadAllText(logFile1, encoding)); Assert.AreEqual("test4", File.ReadAllText(logFile2, encoding)); } finally { Core.Miscellaneous.Utilities.EnsureFileDoesNotExist(logFile1); Core.Miscellaneous.Utilities.EnsureFileDoesNotExist(logFile2); Core.Miscellaneous.Utilities.EnsureFileDoesNotExist(configurationFile); } }
private void HandleParsingErrors(string argumentsAsString, IEnumerable <Error> errors) { var amountOfErrors = errors.Count(); _Log.Log($"Argument '{argumentsAsString}' could not be parsed successfully.", Microsoft.Extensions.Logging.LogLevel.Error); if (0 < amountOfErrors) { _Log.Log($"The following error{(amountOfErrors == 1 ? string.Empty : "s")} occurred:", Microsoft.Extensions.Logging.LogLevel.Error); foreach (var error in errors) { _Log.Log($"{error.Tag}: {_SentenceBuilder.FormatError(error)}", Microsoft.Extensions.Logging.LogLevel.Error); } } }
public void TestLogFileWithRelativePathWithMessageIf() { string logFile = "logfile2.log"; Core.Miscellaneous.Utilities.EnsureFileDoesNotExist(logFile); try { using GRYLog logObject = GRYLog.Create(logFile); logObject.Configuration.SetFormat(GRYLogLogFormat.GRYLogFormat); string file = logFile; Assert.IsFalse(File.Exists(file)); string fileWithRelativePath = logFile; logObject.Configuration.SetLogFile(fileWithRelativePath); Assert.AreEqual(fileWithRelativePath, logObject.Configuration.GetLogFile()); Assert.IsFalse(File.Exists(fileWithRelativePath)); string testContent = "test"; logObject.Log(testContent, LogLevel.Warning, "MyMessageId"); Assert.IsTrue(File.Exists(fileWithRelativePath)); Assert.IsTrue(Regex.IsMatch(File.ReadAllText(logFile), "^\\[\\d\\d\\d\\d-\\d\\d-\\d\\d \\d\\d:\\d\\d:\\d\\d\\] \\[MyMessageId\\] \\[Warning\\] test$")); } finally { Core.Miscellaneous.Utilities.EnsureFileDoesNotExist(logFile); } }
public void TestLogFileWithRelativePath() { string logFile = "logfile.log"; Core.Miscellaneous.Utilities.EnsureFileDoesNotExist(logFile); try { using GRYLog logObject = GRYLog.Create(logFile); logObject.Configuration.SetFormat(GRYLogLogFormat.OnlyMessage); string file = logFile; Assert.IsFalse(File.Exists(file)); string fileWithRelativePath = logFile; logObject.Configuration.SetLogFile(fileWithRelativePath); Assert.AreEqual(fileWithRelativePath, logObject.Configuration.GetLogFile()); Assert.IsFalse(File.Exists(fileWithRelativePath)); string testContent = "test"; logObject.Log(testContent); Assert.IsTrue(File.Exists(fileWithRelativePath)); Assert.AreEqual(testContent, File.ReadAllText(logFile)); } finally { Core.Miscellaneous.Utilities.EnsureFileDoesNotExist(logFile); } }
internal static int Main(string[] arguments) { int result = ExitCodeNoProgramExecuted; string argument = Utilities.GetCommandLineArguments(); string workingDirectory = Directory.GetCurrentDirectory(); try { ParserResult <Options> argumentParserResult = new Parser(settings => settings.CaseInsensitiveEnumValues = true).ParseArguments <Options>(arguments); if (string.IsNullOrEmpty(argument)) { System.Console.WriteLine($"{ProgramName} v{Version}"); System.Console.WriteLine($"Run '{ProgramName} --help' to get help about the usage."); } else if (IsHelpCommand(argument)) { WriteHelp(argumentParserResult); } else { if (argumentParserResult is Parsed <Options> ) { argumentParserResult.WithParsed(options => { result = ProcessArguments(options, result); }); } else if (argumentParserResult is NotParsed <Options> notParsed) { throw new ArgumentException($"Argument '{argument}' could not be parsed successfully. Errors: " + string.Join(",", notParsed.Errors.Select(error => $"\"{error.Tag}: {_SentenceBuilder.FormatError(error)}\""))); } else { throw new ArgumentException($"Argument '{argument}' resulted in a undefined parser-result."); } } } catch (Exception exception) { _Log.Log($"Fatal error occurred while processing argument '{workingDirectory}> epew {argument}", exception); result = ExitCodeFatalErroroccurred; } return(result); }
public void TestLogFileWithRelativePathWithSubFolder() { string folder = "folder"; string logFile = folder + "/logFile.log"; Core.Miscellaneous.Utilities.EnsureFileDoesNotExist(logFile); try { using GRYLog logObject = GRYLog.Create(logFile); logObject.Configuration.SetFormat(GRYLogLogFormat.OnlyMessage); Assert.IsFalse(File.Exists(logFile)); string testContent = "x"; logObject.Log(testContent); Assert.IsTrue(File.Exists(logFile)); Assert.AreEqual(testContent, File.ReadAllText(logFile)); } finally { Core.Miscellaneous.Utilities.EnsureDirectoryDoesNotExist(folder); } }