MemScan-EDR/src-csharp/MemScanEDR/App.xaml.cs

55 lines
1.7 KiB
C#

using System;
using System.IO;
using System.Windows;
using System.Windows.Threading;
namespace MemScanEDR
{
public partial class App : Application
{
private static readonly string LogPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "crash.log");
protected override void OnStartup(StartupEventArgs e)
{
DispatcherUnhandledException += OnDispatcherUnhandledException;
AppDomain.CurrentDomain.UnhandledException += OnDomainUnhandledException;
base.OnStartup(e);
try
{
Directory.CreateDirectory("data");
Directory.CreateDirectory("logs");
Directory.CreateDirectory("dumps");
Directory.CreateDirectory("reports");
}
catch (Exception ex)
{
LogError("Directory creation failed", ex);
}
}
private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
LogError("UI Exception", e.Exception);
MessageBox.Show($"Error logged to crash.log:\n\n{e.Exception.Message}", "MemScan-EDR",
MessageBoxButton.OK, MessageBoxImage.Error);
e.Handled = true;
}
private void OnDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
LogError("Fatal Exception", e.ExceptionObject as Exception);
}
private static void LogError(string context, Exception? ex)
{
try
{
var msg = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {context}\n{ex}\n\n";
File.AppendAllText(LogPath, msg);
}
catch { }
}
}
}