Add desktop diagnostics and resilient message handling
This commit is contained in:
32
WirelessTextSyncer.Windows/Services/AppLogger.cs
Normal file
32
WirelessTextSyncer.Windows/Services/AppLogger.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
namespace WirelessTextSyncer.Windows.Services;
|
||||
|
||||
public static class AppLogger
|
||||
{
|
||||
private static readonly object SyncRoot = new();
|
||||
private static readonly string LogDirectory = Path.Combine(
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
||||
"WirelessTextSyncer");
|
||||
|
||||
public static string LogPath => Path.Combine(LogDirectory, "desktop.log");
|
||||
|
||||
public static void Info(string message)
|
||||
{
|
||||
Write("INFO", message);
|
||||
}
|
||||
|
||||
public static void Error(string message, Exception? exception = null)
|
||||
{
|
||||
Write("ERROR", exception is null ? message : $"{message}{Environment.NewLine}{exception}");
|
||||
}
|
||||
|
||||
private static void Write(string level, string message)
|
||||
{
|
||||
lock (SyncRoot)
|
||||
{
|
||||
Directory.CreateDirectory(LogDirectory);
|
||||
File.AppendAllText(
|
||||
LogPath,
|
||||
$"{DateTimeOffset.Now:yyyy-MM-dd HH:mm:ss.fff zzz} [{level}] {message}{Environment.NewLine}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user