Skip to content

Commit 1dafc9a

Browse files
authored
Dev/Add File Logging (#96)
* Add first iteration of to file logging * Change path * Improve some basics * Improve the basics again * Rework file logging again to create one file per day * Use existing path again, better * Revert previous commit * Add new directory to GitHub Workflow and adjust VSCode
1 parent 9b3996d commit 1dafc9a

File tree

7 files changed

+87
-1
lines changed

7 files changed

+87
-1
lines changed

.github/workflows/dotnet-release-publish.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ jobs:
111111
#wget -qO ./docker-zip/AzzyBot/Modules/MusicStreaming/Files/plugins/java-lyrics-plugin-1.6.4.jar https://maven.lavalink.dev/releases/me/duncte123/java-lyrics-plugin/1.6.4/java-lyrics-plugin-1.6.4.jar
112112
mkdir -p ./docker-zip/AzzyBot/Settings
113113
mv ./AzzyBot-Next/Settings/AzzyBotSettings-Docker.json ./docker-zip/AzzyBot/Settings/AzzyBotSettings-Docker.json
114+
mkdir -p ./docker-zip/AzzyBot/Logs
114115
115116
- name: Archive docker-zip artifact
116117
uses: actions/upload-artifact@v4

.github/workflows/dotnet.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ jobs:
109109
#wget -qO ./docker-zip/AzzyBot/Modules/MusicStreaming/Files/plugins/java-lyrics-plugin-1.6.4.jar https://maven.lavalink.dev/releases/me/duncte123/java-lyrics-plugin/1.6.4/java-lyrics-plugin-1.6.4.jar
110110
mkdir -p ./docker-zip/AzzyBot-Next/Settings
111111
mv ./AzzyBot-Next/Settings/AzzyBotSettings.json ./docker-zip/AzzyBot-Next/Settings/AzzyBotSettings.json
112+
mkdir -p ./docker-zip/AzzyBot-Next/Logs
112113
113114
- name: Archive docker-zip artifact
114115
uses: actions/upload-artifact@v4

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"preLaunchTask": "build-next",
1212
"program": "${workspaceFolder}/artifacts/bin/AzzyBot-Next/Debug/AzzyBot-Dev.dll",
1313
"args": [],
14-
"cwd": "${workspaceFolder}/AzzyBot-Next",
14+
"cwd": "${workspaceFolder}/artifacts/bin/AzzyBot-Next/Debug",
1515
"console": "externalTerminal",
1616
"stopAtEntry": false,
1717
},

AzzyBot-Next/Extensions/ServiceRegistering.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Text;
55
using System.Threading.Tasks;
66
using AzzyBot.Database;
7+
using AzzyBot.Logging;
78
using AzzyBot.Services;
89
using AzzyBot.Services.Modules;
910
using AzzyBot.Settings;
@@ -23,6 +24,11 @@ public static class ServiceRegistering
2324
public static void AzzyBotLogging(this ILoggingBuilder logging, bool isDev = false, bool forceDebug = false)
2425
{
2526
logging.AddConsole();
27+
28+
if (!Directory.Exists("Logs"))
29+
Directory.CreateDirectory("Logs");
30+
31+
logging.AddFile("Logs");
2632
logging.AddFilter("Microsoft.EntityFrameworkCore.Database", (isDev || forceDebug) ? LogLevel.Debug : LogLevel.Warning);
2733
logging.AddFilter("Microsoft.EntityFrameworkCore.Database.Command", (isDev || forceDebug) ? LogLevel.Debug : LogLevel.Warning);
2834
logging.AddFilter("Microsoft.EntityFrameworkCore.Infrastructure", (isDev || forceDebug) ? LogLevel.Debug : LogLevel.Warning);

AzzyBot-Next/Logging/FileLogger.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.IO;
3+
using Microsoft.Extensions.Logging;
4+
5+
namespace AzzyBot.Logging;
6+
7+
public sealed class FileLogger(string directory) : ILogger, IDisposable
8+
{
9+
private readonly string _directory = directory;
10+
private readonly object _lock = new();
11+
12+
public IDisposable? BeginScope<TState>(TState state) where TState : notnull => default!;
13+
public bool IsEnabled(LogLevel logLevel) => true;
14+
private string GetLogFilePath() => Path.Combine(_directory, $"{DateTime.Now:yyyy-MM-dd}.log");
15+
16+
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
17+
{
18+
ArgumentNullException.ThrowIfNull(formatter, nameof(formatter));
19+
20+
if (!IsEnabled(logLevel) || string.IsNullOrWhiteSpace(formatter(state, exception)))
21+
return;
22+
23+
lock (_lock)
24+
{
25+
string message = formatter(state, exception);
26+
string logMessage = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} [{logLevel}] {message}{Environment.NewLine}";
27+
28+
File.AppendAllText(GetLogFilePath(), logMessage);
29+
}
30+
}
31+
32+
public void Dispose() { }
33+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Collections.Concurrent;
2+
using System.Collections.Generic;
3+
using Microsoft.Extensions.Logging;
4+
5+
namespace AzzyBot.Logging;
6+
7+
public sealed class FileLoggerProvider(string directory) : ILoggerProvider
8+
{
9+
private readonly string _directory = directory;
10+
private readonly ConcurrentDictionary<string, FileLogger> _loggers = new();
11+
12+
public ILogger CreateLogger(string categoryName) => _loggers.GetOrAdd(categoryName, name => new FileLogger(_directory));
13+
public void Dispose() => Dispose(true);
14+
15+
public void Dispose(bool disposing)
16+
{
17+
if (!disposing)
18+
return;
19+
20+
foreach (KeyValuePair<string, FileLogger> logger in _loggers)
21+
{
22+
logger.Value.Dispose();
23+
}
24+
25+
_loggers.Clear();
26+
}
27+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.DependencyInjection.Extensions;
4+
using Microsoft.Extensions.Logging;
5+
6+
namespace AzzyBot.Logging;
7+
8+
public static class LoggingBuilderExtensions
9+
{
10+
public static ILoggingBuilder AddFile(this ILoggingBuilder builder, string directory)
11+
{
12+
ArgumentNullException.ThrowIfNull(builder, nameof(builder));
13+
14+
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, FileLoggerProvider>(p => new FileLoggerProvider(directory)));
15+
16+
return builder;
17+
}
18+
}

0 commit comments

Comments
 (0)