mirror of
https://github.com/ZuneDev/ZuneModdingHelper.git
synced 2026-07-27 13:12:21 -07:00
Add logging
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using OwlCore.AbstractUI.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using OwlCore.AbstractUI.Models;
|
||||
using OwlCore.ComponentModel;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -28,7 +29,7 @@ public class WebservicesModFactory : DIModFactoryBase<WebservicesMod>
|
||||
Description, Author, new(1, 2));
|
||||
}
|
||||
|
||||
public partial class WebservicesMod(ModMetadata metadata, IModCoreConfig modConfig) : Mod(metadata), IAsyncInit
|
||||
public partial class WebservicesMod(ModMetadata metadata, IModCoreConfig modConfig, ILogger<WebservicesMod> log) : Mod(metadata), IAsyncInit
|
||||
{
|
||||
private const int ZUNESERVICES_ENDPOINTS_BLOCK_OFFSET = 0x14D60;
|
||||
private const int ZUNESERVICES_ENDPOINTS_BLOCK_LENGTH = 0x884;
|
||||
@@ -130,6 +131,8 @@ public partial class WebservicesMod(ModMetadata metadata, IModCoreConfig modConf
|
||||
|
||||
if (errorMessage is not null)
|
||||
{
|
||||
log.LogWarning("Binary patching failed, falling back to hosts file modification: {ErrorMessage}", errorMessage);
|
||||
|
||||
// Fallback to editing hosts file
|
||||
errorMessage = await AddEntriesToHostsFile(newHost);
|
||||
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
|
||||
namespace ZuneModCore.Services;
|
||||
|
||||
public class FileLoggerProvider(string logDirectory) : ILoggerProvider
|
||||
{
|
||||
private StreamWriter? _writer;
|
||||
|
||||
public ILogger CreateLogger(string categoryName)
|
||||
{
|
||||
_writer ??= CreateLogFile();
|
||||
|
||||
return new FileLogger(_writer, categoryName);
|
||||
}
|
||||
|
||||
private StreamWriter CreateLogFile()
|
||||
{
|
||||
var logFilePath = Path.Combine(logDirectory, $"{DateTime.Now:yyyyMMdd_HHmmss}.log");
|
||||
var logFile = File.Open(logFilePath, FileMode.CreateNew, FileAccess.Write, FileShare.Read);
|
||||
|
||||
return new(logFile) { AutoFlush = true };
|
||||
}
|
||||
|
||||
public void Dispose() => _writer?.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="FileLogger"/> class.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the logger.</param>
|
||||
internal sealed class FileLogger(TextWriter writer, string name) : ILogger
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public IDisposable BeginScope<TState>(TState state) where TState : notnull => null!;
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool IsEnabled(LogLevel logLevel) => logLevel != LogLevel.None;
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
|
||||
{
|
||||
if (!IsEnabled(logLevel))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ArgumentNullException.ThrowIfNull(formatter);
|
||||
|
||||
string formatted = formatter(state, exception);
|
||||
|
||||
if (string.IsNullOrEmpty(formatted) && exception == null)
|
||||
{
|
||||
// With no formatted message or exception, there's nothing to print.
|
||||
return;
|
||||
}
|
||||
|
||||
var message = $"{logLevel} [{name}]{Environment.NewLine}";
|
||||
if (string.IsNullOrEmpty(formatted))
|
||||
{
|
||||
Debug.Assert(exception != null);
|
||||
message += $"{exception}";
|
||||
}
|
||||
else if (exception == null)
|
||||
{
|
||||
message += $"{formatted}";
|
||||
}
|
||||
else
|
||||
{
|
||||
message += $"{formatted}{Environment.NewLine}{Environment.NewLine}{exception}";
|
||||
}
|
||||
|
||||
writer.WriteLine(message);
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.8" />
|
||||
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
|
||||
<PackageReference Include="OwlCore" Version="0.4.1" />
|
||||
<PackageReference Include="OwlCore.AbstractUI" Version="0.0.0" />
|
||||
|
||||
Reference in New Issue
Block a user