| 1 | #nullable enable |
| 2 | using System.Globalization; |
| 3 | using CascadeIDE.Contracts; |
| 4 | |
| 5 | namespace CascadeIDE.Features.Workspace.Application; |
| 6 | |
| 7 | /// <summary> |
| 8 | /// Локальный журнал при необработанном исключении загрузки решения (<c>.cascade-ide/crash-log.txt</c> рядом с .sln или в текущем каталоге). |
| 9 | /// </summary> |
| 10 | [ComputingUnit("workspace-solution-load-crash-log")] |
| 11 | public static class SolutionLoadCrashLog |
| 12 | { |
| 13 | public static void TryAppend(string? solutionPath, Exception ex) |
| 14 | { |
| 15 | try |
| 16 | { |
| 17 | var baseDir = ""; |
| 18 | if (!string.IsNullOrWhiteSpace(solutionPath)) |
| 19 | { |
| 20 | try |
| 21 | { |
| 22 | var full = CanonicalFilePath.Normalize(solutionPath); |
| 23 | baseDir = File.Exists(full) ? (Path.GetDirectoryName(full) ?? "") : full; |
| 24 | } |
| 25 | catch |
| 26 | { |
| 27 | baseDir = ""; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | if (string.IsNullOrWhiteSpace(baseDir)) |
| 32 | baseDir = Environment.CurrentDirectory; |
| 33 | |
| 34 | var logDir = Path.Combine(baseDir, ".cascade-ide"); |
| 35 | Directory.CreateDirectory(logDir); |
| 36 | var logPath = Path.Combine(logDir, "crash-log.txt"); |
| 37 | var stamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff 'UTC'", CultureInfo.InvariantCulture); |
| 38 | var payload = |
| 39 | $"[{stamp}] LoadSolution crash{Environment.NewLine}" + |
| 40 | $"solution: {solutionPath}{Environment.NewLine}" + |
| 41 | $"{ex}{Environment.NewLine}" + |
| 42 | $"---{Environment.NewLine}"; |
| 43 | File.AppendAllText(logPath, payload); |
| 44 | } |
| 45 | catch |
| 46 | { |
| 47 | // Do not throw from crash logger. |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |