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

Feature/file logging #463

Merged
merged 4 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
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.None);

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
Loading