Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure logging in code instead of through log4net.config #364

Merged
merged 2 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
5 changes: 1 addition & 4 deletions commonItems/commonItems.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
<PackageId>PGCG.$(AssemblyName)</PackageId>
<Version>8.4.2</Version>
<Version>9.0.0</Version>
<Authors>PGCG</Authors>
<PackageProjectUrl>https://github.com/ParadoxGameConverters/commonItems.NET</PackageProjectUrl>
<RepositoryUrl>https://github.com/ParadoxGameConverters/commonItems.NET</RepositoryUrl>
Expand Down 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"
}
}
Loading