-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add minidump generation, more error handling and logging
- Loading branch information
1 parent
6f36483
commit 31f9706
Showing
8 changed files
with
266 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
using System.IO; | ||
using System.Diagnostics; | ||
|
||
class DumpCreator | ||
{ | ||
[Flags] | ||
public enum Typ : uint | ||
{ | ||
// Add the MiniDump flags you need, for example: | ||
MiniDumpNormal = 0x00000000, | ||
MiniDumpWithDataSegs = 0x00000001, | ||
// etc. | ||
} | ||
|
||
[DllImport("DbgHelp.dll")] | ||
public static extern bool MiniDumpWriteDump(IntPtr hProcess, uint ProcessId, SafeHandle hFile, Typ DumpType, | ||
IntPtr ExceptionParam, IntPtr UserStreamParam, IntPtr CallbackParam); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
|
||
public static class Logger | ||
{ | ||
private static readonly object lockObj = new(); | ||
private static string? logFilePath; | ||
|
||
private static string GetLogFilePath() | ||
{ | ||
if (logFilePath == null) | ||
{ | ||
var fileName = $"app_{DateTime.Now:yyyy-MM-dd}.log"; | ||
logFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName); | ||
} | ||
return logFilePath; | ||
} | ||
|
||
|
||
public static void CleanupOldLogFiles() | ||
{ | ||
var directory = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory); | ||
var oldFiles = directory.GetFiles("app_*.log") | ||
.Where(f => f.CreationTime < DateTime.Now.AddDays(-7)) | ||
.ToList(); | ||
|
||
foreach (var file in oldFiles) | ||
{ | ||
try | ||
{ | ||
file.Delete(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Error("Failed to delete log file", ex); | ||
} | ||
} | ||
} | ||
|
||
|
||
public static void Info(string message) | ||
{ | ||
Log("INFO", message); | ||
} | ||
|
||
public static void Warn(string message) | ||
{ | ||
Log("WARN", message); | ||
} | ||
|
||
public static void Error(string message, Exception? ex = null) | ||
{ | ||
var logMessage = new StringBuilder(message); | ||
if (ex != null) | ||
{ | ||
logMessage.AppendLine(); // Ensure the exception starts on a new line | ||
logMessage.AppendLine($"Exception: {ex.Message}"); | ||
logMessage.AppendLine($"StackTrace: {ex.StackTrace}"); | ||
} | ||
|
||
Log("ERROR", logMessage.ToString()); | ||
} | ||
|
||
public static void Fatal(string message, Exception? ex = null) | ||
{ | ||
var logMessage = new StringBuilder(message); | ||
if (ex != null) | ||
{ | ||
logMessage.AppendLine(); // Ensure the exception starts on a new line | ||
logMessage.AppendLine($"Exception: {ex.Message}"); | ||
logMessage.AppendLine($"StackTrace: {ex.StackTrace}"); | ||
} | ||
Log("FATAL", logMessage.ToString()); | ||
} | ||
|
||
private static void Log(string level, string message) | ||
{ | ||
try | ||
{ | ||
lock (lockObj) | ||
{ | ||
using var sw = new StreamWriter(GetLogFilePath(), true, Encoding.UTF8); | ||
sw.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} [{level}] {message}"); | ||
} | ||
} | ||
catch | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.