Repository navigation
Expand file tree
/
Copy pathFatalErrorLog.cs
More file actions
49 lines (43 loc) · 1.72 KB
/
Copy pathFatalErrorLog.cs
File metadata and controls
49 lines (43 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.IO;
namespace MissionController.Core;
/// <summary>
/// Last-resort crash sink for failures that kill the process before any UI exists.
///
/// This lives in Core rather than inside the App class for one reason: a crash logger that cannot be
/// tested is a crash logger you find out about during the crash. Keeping it free of WinUI types lets
/// the suite prove it appends, creates its directory, and - most importantly - stays silent when it
/// cannot write at all.
/// </summary>
public static class FatalErrorLog
{
/// <summary>Where the app writes startup and unhandled-exception detail by default.</summary>
public static string DefaultPath => SplatDataRoot.Layout.CrashLog;
/// <summary>
/// Appends one fatal-error record. Returns whether it reached disk, so a caller that cares can
/// tell - but callers on the crash path should ignore it and carry on.
///
/// Never throws. A logger that raises during crash handling replaces a diagnosable failure with
/// an undiagnosable one, which is strictly worse than losing the record.
/// </summary>
public static bool Write(string source, object? detail, string? path = null)
{
try
{
string target = path ?? DefaultPath;
string? directory = Path.GetDirectoryName(target);
if (!string.IsNullOrEmpty(directory))
{
Directory.CreateDirectory(directory);
}
File.AppendAllText(
target,
$"[{DateTime.UtcNow:O}] {source}{Environment.NewLine}{detail}{Environment.NewLine}{Environment.NewLine}");
return true;
}
catch
{
return false;
}
}
}