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); } }
private SupervisedThread(Action action, GRYLog log, string name, string informationAboutInvoker) { this.InformationAboutInvoker = informationAboutInvoker; this.Action = action; this.Id = Guid.NewGuid(); this.Name = $"{nameof(SupervisedThread)} {this.Id} " + (string.IsNullOrEmpty(name) ? string.Empty : $"({name})"); this.LogObject = log; }
protected override void ExecuteImplementation(LogItem logItem, GRYLog logObject) { logItem.Format(logObject.Configuration, out string formattedMessage, out int _, out int _, out ConsoleColor _, this.Format, logItem.MessageId); this._Pool.Add(formattedMessage); if (this.PreFlushPoolSize <= this._Pool.Count) { this.Flush(); } }
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); } }
public void TestLogProgress() { GRYLog logObject = GRYLog.Create(); logObject.Configuration.StoreProcessedLogItemsInternally = true; logObject.LogProgress(0, 4); logObject.LogProgress(3, 122); logObject.LogProgress(73, 73); Assert.AreEqual(3, logObject.ProcessedLogItems.Count); Assert.AreEqual("Processed 0/4 items (0%)", logObject.ProcessedLogItems[0].PlainMessage); Assert.AreEqual("Processed 003/122 items (2,46%)", logObject.ProcessedLogItems[1].PlainMessage); Assert.AreEqual("Processed 73/73 items (100%)", logObject.ProcessedLogItems[2].PlainMessage); }
public static GitCommandResult ExecuteGitCommand(string repositoryFolder, string argument, bool throwErrorIfExitCodeIsNotZero = false, int?timeoutInMilliseconds = null, bool printErrorsAsInformation = false, bool writeOutputToConsole = false) { using GRYLog log = GRYLog.Create(); log.Configuration.Enabled = true; log.Configuration.SetEnabledOfAllLogTargets(writeOutputToConsole); using ExternalProgramExecutor externalProgramExecutor = new("git", argument, repositoryFolder) { LogObject = log, TimeoutInMilliseconds = timeoutInMilliseconds, PrintErrorsAsInformation = printErrorsAsInformation, ThrowErrorIfExitCodeIsNotZero = throwErrorIfExitCodeIsNotZero }; externalProgramExecutor.StartSynchronously(); return(new GitCommandResult(argument, repositoryFolder, externalProgramExecutor.AllStdOutLines, externalProgramExecutor.AllStdErrLines, externalProgramExecutor.ExitCode)); }
protected override void ExecuteImplementation(LogItem logItem, GRYLog logObject) { TextWriter output; if (logItem.IsErrorEntry()) { output = System.Console.Error; } else { output = System.Console.Out; } logItem.Format(logObject.Configuration, out string formattedMessage, out int cb, out int ce, out ConsoleColor _, this.Format, logItem.MessageId); output.Write(formattedMessage.Substring(0, cb)); this.WriteWithColorToConsole(formattedMessage[cb..ce], output, logItem.LogLevel, logObject);
protected override void ExecuteImplementation(LogItem logItem, GRYLog logObject) { using EventLog eventLog = new(Utilities.GetNameOfCurrentExecutable()) { Source = logObject.Configuration.Name }; string messageId; if (string.IsNullOrWhiteSpace(logItem.MessageId)) { messageId = string.Empty; } else { messageId = $"{logItem.MessageId}: "; } eventLog.WriteEntry(messageId + logItem.PlainMessage, ConvertLogLevel(logItem.LogLevel), logItem.EventId, logItem.Category); }
protected override void ExecuteImplementation(LogItem logItem, GRYLog logObject) { string messageId; if (string.IsNullOrWhiteSpace(logItem.MessageId)) { messageId = string.Empty; } else { messageId = $"--rfc5424 --msgid {logItem.MessageId}"; } using ExternalProgramExecutor externalProgramExecutor = new("Logger", $"--tag {Utilities.GetNameOfCurrentExecutable()} {messageId} -- [{logItem.LogLevel}] [{logObject.Configuration.Name}] {logItem.PlainMessage}") { ThrowErrorIfExitCodeIsNotZero = true }; externalProgramExecutor.StartSynchronously(); }
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); } }
protected override void ExecuteImplementation(LogItem logItem, GRYLog logObject) { logObject.InvokeObserver(logItem); }
public static SupervisedThread CreateByGRYLog(Action action, GRYLog log = null, string name = "", string informationAboutInvoker = "") { return(new SupervisedThread(action, log, name, informationAboutInvoker)); }
public static SupervisedThread CreateByLogFile(Action action, string logFile, string name = "", string informationAboutInvoker = "") { return(CreateByGRYLog(action, GRYLog.Create(logFile), name, informationAboutInvoker)); }
public EventSender(GRYLog logObject) { this.LogObject = logObject; this.Initialize(default);