Skip to content

Commit

Permalink
Feature/file logging (#463)
Browse files Browse the repository at this point in the history
* Implement logging provider to dump logs to a stream. Make Obsidian.ConsoleApp dump logs to a FileStream.

* Add more detailed exceptions to logs

* Allow log files to be read while they are being written
  • Loading branch information
Naamloos authored Dec 11, 2024
1 parent 24a54c6 commit 5b2fa46
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Obsidian.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@

builder.ConfigureObsidian();

if(!Directory.Exists("logs"))
{
Directory.CreateDirectory("logs");
}
// filename with date,time
var logFile = $"logs/{DateTime.Now:yyyy-MM-dd_HH-mm-ss}.log";
var logFileStream = new FileStream(logFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);

builder.Services.AddLogging(loggingBuilder =>
{
loggingBuilder.ClearProviders();
Expand All @@ -40,6 +48,8 @@
x.IncludeScopes = true;
x.TimestampFormat = "HH:mm:ss ";
});

loggingBuilder.AddProvider(new StreamLoggerProvider(logFileStream));
});

builder.AddObsidian();
Expand Down
1 change: 1 addition & 0 deletions Obsidian/Hosting/DefaultServerEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Task IServerEnvironment.OnServerStoppedGracefullyAsync()
Task IServerEnvironment.OnServerCrashAsync(Exception e)
{
// Write crash log somewhere?
// FileLogger implemented in ConsoleApp
var byeMessages = new[]
{
"We had a good run...",
Expand Down
74 changes: 74 additions & 0 deletions Obsidian/Hosting/StreamLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Obsidian.Hosting;
public class StreamLoggerProvider : ILoggerProvider
{
private readonly StreamWriter _streamWriter;

public StreamLoggerProvider(Stream stream)
{
_streamWriter = new StreamWriter(stream);
}

public ILogger CreateLogger(string category)
{
return new StreamLogger(category, _streamWriter);
}

public void Dispose()
{
_streamWriter.Dispose();
}
}

public class StreamLogger : ILogger
{
private readonly string _category;
private readonly StreamWriter _streamWriter;

public StreamLogger(string category, StreamWriter streamWriter)
{
_category = category;
_streamWriter = streamWriter;
}

public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null;
public bool IsEnabled(LogLevel logLevel)
{
return true;
}

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
if (!IsEnabled(logLevel))
{
return;
}

var msg = formatter(state, exception);

if (string.IsNullOrEmpty(msg))
{
return;
}

var dateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

if (exception != null)
{
_streamWriter.WriteLine("[{0}] [{1}] ({2})\n--------------------\n{3}\n--------------------", dateTime, logLevel, _category, exception.ToString());
}
else
{
_streamWriter.WriteLine("[{0}] [{1}] ({2}) {3}", dateTime, logLevel, _category, msg);
}

_streamWriter.Flush();
}
}
4 changes: 4 additions & 0 deletions Obsidian/Registries/StructureRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ internal static class StructureRegistry
public static void Initialize()
{
var structDir = "Assets/Structures/";
if(!Directory.Exists("Assets/Structures/"))
{
Directory.CreateDirectory("Assets/Structures");
}
var files = Directory.GetFiles(structDir, "*.nbt");
foreach (var file in files)
{
Expand Down

0 comments on commit 5b2fa46

Please sign in to comment.