From 2c47897c6186fbd45c9d53760e7d25a10c14429a Mon Sep 17 00:00:00 2001 From: IhateTrains Date: Sun, 22 Oct 2023 02:20:12 +0100 Subject: [PATCH] Configure logging in code instead of through log4net.config (#364) * Configure logging in code instead of through log4net.config * Bump version --- commonItems/Logger.cs | 60 +++++++++++++++++++++++++++++----- commonItems/commonItems.csproj | 5 +-- commonItems/log4net.config | 23 ------------- global.json | 2 +- 4 files changed, 53 insertions(+), 37 deletions(-) delete mode 100644 commonItems/log4net.config diff --git a/commonItems/Logger.cs b/commonItems/Logger.cs index f1f3ab42..8d5fb4d5 100644 --- a/commonItems/Logger.cs +++ b/commonItems/Logger.cs @@ -1,8 +1,9 @@ using log4net; -using log4net.Config; +using log4net.Appender; using log4net.Core; +using log4net.Layout; +using log4net.Repository.Hierarchy; using System.Diagnostics.CodeAnalysis; -using System.IO; // ReSharper disable InconsistentNaming @@ -11,51 +12,90 @@ namespace commonItems; [SuppressMessage("ReSharper", "IntroduceOptionalParameters.Global")] public static class Logger { private static readonly ILog log = LogManager.GetLogger("mainLogger"); + static Logger() { - Configure("log4net.config"); + Configure(); } - public static void Configure(string log4netConfigPath) { + public static void Configure(bool logToConsole = true, bool logToFile = false) { var repository = LogManager.GetRepository(); if (repository.Configured) { return; } - // add custom "PROGRESS" level + var hierarchy = (Hierarchy)repository; + hierarchy.Root.RemoveAllAppenders(); + + // Add custom "PROGRESS" level. repository.LevelMap.Add(LogExtensions.ProgressLevel); - // configure log4net - var logConfiguration = new FileInfo(log4netConfigPath); - XmlConfigurator.Configure(logConfiguration); + var layout = new PatternLayout { + ConversionPattern = "%date{yyyy'-'MM'-'dd HH':'mm':'ss} [%level] %message%newline", + }; + layout.ActivateOptions(); + if (logToConsole) { + var consoleAppender = new ConsoleAppender { + Threshold = Level.All, Target = "Console.Out", Layout = layout, + }; + consoleAppender.ActivateOptions(); + hierarchy.Root.AddAppender(consoleAppender); + } + if (logToFile) { + var fileAppender = new RollingFileAppender { + Name = "file", + File = "log.txt", + AppendToFile = true, + RollingStyle = RollingFileAppender.RollingMode.Size, + MaxSizeRollBackups = 5, + MaximumFileSize = "100MB", + StaticLogFileName = true, + Layout = layout, + Threshold = Level.All, + }; + fileAppender.ActivateOptions(); + hierarchy.Root.AddAppender(fileAppender); + } + + hierarchy.Root.Level = Level.All; + hierarchy.Configured = true; } public static void Error(string message) { log.Error(message); } + public static void ErrorFormat(string message, params object[] args) { log.ErrorFormat(message, args); } + public static void Warn(string message) { log.Warn(message); } + public static void WarnFormat(string message, params object[] args) { log.WarnFormat(message, args); } + public static void Info(string message) { log.Info(message); } + public static void InfoFormat(string message, params object[] args) { log.InfoFormat(message, args); } + public static void Notice(string message) { log.Notice(message); } + public static void NoticeFormat(string message, params object[] args) { log.NoticeFormat(message, args); } + public static void Debug(string message) { log.Debug(message); } + public static void DebugFormat(string message, params object[] args) { log.DebugFormat(message, args); } @@ -63,9 +103,11 @@ public static void DebugFormat(string message, params object[] args) { public static void Progress(int progressValue) { log.Progress(progressValue); } + public static void IncrementProgress() { log.IncrementProgress(); } + public static void IncrementProgress(int progressLimit) { log.IncrementProgress(progressLimit); } @@ -73,4 +115,4 @@ public static void IncrementProgress(int progressLimit) { public static void Log(Level level, string message) { log.Log(level, message); } -} +} \ No newline at end of file diff --git a/commonItems/commonItems.csproj b/commonItems/commonItems.csproj index d89cccf7..6442fb2d 100644 --- a/commonItems/commonItems.csproj +++ b/commonItems/commonItems.csproj @@ -6,7 +6,7 @@ False PGCG.$(AssemblyName) - 8.4.2 + 9.0.0 PGCG https://github.com/ParadoxGameConverters/commonItems.NET https://github.com/ParadoxGameConverters/commonItems.NET @@ -42,9 +42,6 @@ - - PreserveNewest - PreserveNewest diff --git a/commonItems/log4net.config b/commonItems/log4net.config deleted file mode 100644 index 893a0d21..00000000 --- a/commonItems/log4net.config +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/global.json b/global.json index ce7879df..1d6b803d 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "sdk": { - "version": "7.0.401", + "version": "7.0.402", "rollForward": "disable" } }