Skip to content

Commit

Permalink
Configure logging in code instead of through log4net.config
Browse files Browse the repository at this point in the history
  • Loading branch information
IhateTrains committed Oct 22, 2023
1 parent 2e4371f commit 19324f1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 36 deletions.
60 changes: 51 additions & 9 deletions commonItems/Logger.cs
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -11,66 +12,107 @@ 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);
}

public static void Progress(int progressValue) {
log.Progress(progressValue);
}

public static void IncrementProgress() {
log.IncrementProgress();
}

public static void IncrementProgress(int progressLimit) {
log.IncrementProgress(progressLimit);
}

public static void Log(Level level, string message) {
log.Log(level, message);
}
}
}
3 changes: 0 additions & 3 deletions commonItems/commonItems.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@
</ItemGroup>

<ItemGroup>
<None Update="log4net.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="UnitTests\TestFiles\broken-settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
23 changes: 0 additions & 23 deletions commonItems/log4net.config

This file was deleted.

2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "7.0.401",
"version": "7.0.402",
"rollForward": "disable"
}
}

0 comments on commit 19324f1

Please sign in to comment.