Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions src/Snipdeck.App/CrashLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal static class CrashLog
{
private const string _logFileName = "unhandled.log";
private const int _maxLogBytes = 5 * 1024 * 1024;
private static readonly object _writeLock = new();
private static readonly Lock _writeLock = new();

public static void Write(string source, Exception exception)
{
Expand All @@ -31,7 +31,7 @@ public static void Write(string source, Exception exception)
return;
}

Directory.CreateDirectory(paths.LogsDirectory);
_ = Directory.CreateDirectory(paths.LogsDirectory);
var logPath = Path.Combine(paths.LogsDirectory, _logFileName);
var text = Format(source, exception);

Expand Down Expand Up @@ -74,41 +74,41 @@ private static void RotateIfTooLarge(string logPath)
private static string Format(string source, Exception exception)
{
var sb = new StringBuilder();
sb.AppendLine(new string('=', 72));
sb.Append(DateTimeOffset.UtcNow.ToString("O", CultureInfo.InvariantCulture))
.Append(" ")
.AppendLine(source);
_ = sb.AppendLine(new string('=', 72));
_ = sb.Append(DateTimeOffset.UtcNow.ToString("O", CultureInfo.InvariantCulture))
.Append(" ")
.AppendLine(source);
AppendException(sb, exception, depth: 0);
sb.AppendLine();
_ = sb.AppendLine();
return sb.ToString();
}

private static void AppendException(StringBuilder sb, Exception ex, int depth)
{
var indent = new string(' ', depth * 2);

sb.Append(indent).Append("Type: ").AppendLine(ex.GetType().FullName ?? ex.GetType().Name);
_ = sb.Append(indent).Append("Type: ").AppendLine(ex.GetType().FullName ?? ex.GetType().Name);
if (ex is COMException)
{
sb.Append(indent).Append("HRESULT: 0x")
.AppendLine(ex.HResult.ToString("X8", CultureInfo.InvariantCulture));
_ = sb.Append(indent).Append("HRESULT: 0x")
.AppendLine(ex.HResult.ToString("X8", CultureInfo.InvariantCulture));
}
sb.Append(indent).Append("Message: ").AppendLine(ex.Message);
_ = sb.Append(indent).Append("Message: ").AppendLine(ex.Message);
if (!string.IsNullOrEmpty(ex.Source))
{
sb.Append(indent).Append("Source: ").AppendLine(ex.Source);
_ = sb.Append(indent).Append("Source: ").AppendLine(ex.Source);
}
if (!string.IsNullOrWhiteSpace(ex.StackTrace))
{
sb.Append(indent).AppendLine("Stack:");
foreach (var line in ex.StackTrace!.Split('\n'))
_ = sb.Append(indent).AppendLine("Stack:");
foreach (var line in ex.StackTrace.Split('\n'))
{
sb.Append(indent).Append(" ").AppendLine(line.TrimEnd('\r'));
_ = sb.Append(indent).Append(" ").AppendLine(line.TrimEnd('\r'));
}
}
if (ex.InnerException is not null)
{
sb.Append(indent).AppendLine("Inner:");
_ = sb.Append(indent).AppendLine("Inner:");
AppendException(sb, ex.InnerException, depth + 1);
}
}
Expand Down
Loading