1
0
mirror of synced 2024-11-30 14:14:29 +01:00
TaikoSoundEditor/Logger.cs

62 lines
1.8 KiB
C#
Raw Normal View History

2023-07-19 07:10:25 +02:00
using System.Collections;
namespace TaikoSoundEditor
{
internal static class Logger
{
static string LogFile;
static Logger()
{
if (!Directory.Exists("logs"))
Directory.CreateDirectory("logs");
LogFile = $"tse_{DateTime.Now:yyyy-MM-dd_hh-mm-ss}.log";
LogFile = Path.Combine("logs", LogFile);
Info("Session started");
}
private static void Write(string message)
{
try
{
File.AppendAllText(LogFile, message);
}
catch (Exception e)
{
MessageBox.Show("Failed to write log file:\n" + e.Message);
}
}
private static void Write(string type, string message)
{
Write($"[{type} {DateTime.Now:yyyy-MM-dd_hh-mm-ss}]{message}\n");
}
public static void Info(string message)=> Write("INFO", message);
public static void Warning(string message)=> Write("WARN", message);
public static void Error(string message)=> Write("ERROR", message);
public static void Error(Exception e)
{
Error($"Exception raised:\n{e.Message}\n{e.StackTrace}\nFrom {e.Source}\nData:\n");
foreach(DictionaryEntry kv in e.Data)
{
Write($"{kv.Key} = {kv.Value}");
}
Write("\n\n");
if (e.GetBaseException() != null && e.GetBaseException() != e)
{
Error(e.GetBaseException());
Write("Base exception:");
}
if(e.InnerException!=null)
{
Write("Inner exception:");
Error(e.InnerException);
}
}
}
}