-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugSessionLog.cs
More file actions
64 lines (57 loc) · 1.55 KB
/
DebugSessionLog.cs
File metadata and controls
64 lines (57 loc) · 1.55 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System.Text.Json;
namespace WorkshopUploader;
/// <summary>NDJSON debug log (Debug builds only; stripped from Release for size and I/O).</summary>
internal static class DebugSessionLog
{
#if DEBUG
private const string SessionId = "9fc458";
private static readonly string LogPath = ResolveLogPath();
private static readonly object Gate = new();
internal static string LogFilePath => LogPath;
private static string ResolveLogPath()
{
try
{
var dir = new DirectoryInfo(AppContext.BaseDirectory);
for (var i = 0; i < 14 && dir != null; i++, dir = dir.Parent)
{
var csproj = Path.Combine(dir.FullName, "WorkshopUploader", "WorkshopUploader.csproj");
if (File.Exists(csproj))
return Path.Combine(dir.FullName, "debug-9fc458.log");
}
}
catch
{
// ignored
}
return Path.Combine(Path.GetTempPath(), "debug-9fc458.log");
}
public static void Write(string hypothesisId, string location, string message, object? data = null)
{
try
{
var line = JsonSerializer.Serialize(new
{
sessionId = SessionId,
hypothesisId,
location,
message,
timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
runId = Environment.GetEnvironmentVariable("WORKSHOP_UPLOADER_DEBUG_RUN") ?? "pre-fix",
data,
});
lock (Gate)
{
File.AppendAllText(LogPath, line + Environment.NewLine);
}
}
catch
{
// never break startup
}
}
#else
internal static string LogFilePath => string.Empty;
public static void Write(string hypothesisId, string location, string message, object? data = null) { }
#endif
}