mirror of
https://github.com/BlossomiShymae/Needlework.Net.git
synced 2025-12-06 10:10:48 +01:00
refactor: logging
This commit is contained in:
31
Needlework.Net/Logger.cs
Normal file
31
Needlework.Net/Logger.cs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Serilog;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Needlework.Net
|
||||||
|
{
|
||||||
|
public static class Logger
|
||||||
|
{
|
||||||
|
public static void Setup(ILoggingBuilder builder)
|
||||||
|
{
|
||||||
|
var logger = new LoggerConfiguration()
|
||||||
|
.MinimumLevel.Debug()
|
||||||
|
.WriteTo.File("Logs/debug-.log", rollingInterval: RollingInterval.Day, shared: true)
|
||||||
|
.CreateLogger();
|
||||||
|
logger.Debug("NeedleworkDotNet version: {Version}", Assembly.GetEntryAssembly()?.GetName().Version?.ToString() ?? "0.0.0.0");
|
||||||
|
logger.Debug("OS description: {Description}", System.Runtime.InteropServices.RuntimeInformation.OSDescription);
|
||||||
|
builder.AddSerilog(logger);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void LogFatal(UnhandledExceptionEventArgs e)
|
||||||
|
{
|
||||||
|
File.WriteAllText($"Logs/fatal-{DateTime.Now:HHmmssfff}.log", e.ExceptionObject.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -48,6 +48,13 @@
|
|||||||
<AvaloniaResource Include="Assets\**" />
|
<AvaloniaResource Include="Assets\**" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<AvaloniaXaml Remove="Utilities\**" />
|
||||||
|
<Compile Remove="Utilities\**" />
|
||||||
|
<EmbeddedResource Remove="Utilities\**" />
|
||||||
|
<None Remove="Utilities\**" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<UpToDateCheckInput Remove="Views\AboutView.axaml" />
|
<UpToDateCheckInput Remove="Views\AboutView.axaml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
@@ -69,6 +76,5 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Assets\Users\" />
|
<Folder Include="Assets\Users\" />
|
||||||
<Folder Include="Utilities\" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -51,14 +51,7 @@ class Program
|
|||||||
builder.AddSingleton<DialogService>();
|
builder.AddSingleton<DialogService>();
|
||||||
builder.AddSingletonsFromAssemblies<PageBase>();
|
builder.AddSingletonsFromAssemblies<PageBase>();
|
||||||
builder.AddHttpClient();
|
builder.AddHttpClient();
|
||||||
|
builder.AddLogging(Logger.Setup);
|
||||||
var logger = new LoggerConfiguration()
|
|
||||||
.MinimumLevel.Debug()
|
|
||||||
.WriteTo.File("Logs/NeedleworkDotNet.log", rollingInterval: RollingInterval.Day, shared: true)
|
|
||||||
.CreateLogger();
|
|
||||||
logger.Debug("NeedleworkDotNet version: {Version}", Assembly.GetEntryAssembly()?.GetName().Version?.ToString() ?? "0.0.0.0");
|
|
||||||
logger.Debug("OS description: {Description}", System.Runtime.InteropServices.RuntimeInformation.OSDescription);
|
|
||||||
builder.AddLogging(builder => builder.AddSerilog(logger));
|
|
||||||
|
|
||||||
var services = builder.BuildServiceProvider();
|
var services = builder.BuildServiceProvider();
|
||||||
return services;
|
return services;
|
||||||
@@ -66,6 +59,6 @@ class Program
|
|||||||
|
|
||||||
private static void Program_UnhandledException(object sender, UnhandledExceptionEventArgs e)
|
private static void Program_UnhandledException(object sender, UnhandledExceptionEventArgs e)
|
||||||
{
|
{
|
||||||
File.WriteAllText($"errorlog-{DateTime.Now:HHmmssfff}", e.ExceptionObject.ToString());
|
Logger.LogFatal(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user