Skip to content

Commit 4e6d709

Browse files
committed
reuse stringbuilder for log file appending
1 parent 9dd2288 commit 4e6d709

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

src/Bot/Helpers/Logger.Internal.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,54 +60,56 @@ public static void Log(LogSeverity s, LogSource from, string message, Exception
6060
Error = e,
6161
Invocation = caller
6262
});
63+
64+
private static StringBuilder _logFileBuilder = new();
6365

6466
private static void Execute(LogSeverity s, LogSource src, string message, Exception e, InvocationInfo caller)
6567
{
66-
var content = new StringBuilder();
67-
6868
if (IsDebugLoggingEnabled && caller.IsInitialized)
6969
{
7070
caller.ToString().IfPresent(debugInfoContent =>
7171
{
7272
// ReSharper disable once AccessToModifiedClosure
73-
Append(debugInfoContent, Color.Aquamarine, ref content);
74-
Append(" |> ", Color.Goldenrod, ref content);
73+
Append(debugInfoContent, Color.Aquamarine, ref _logFileBuilder);
74+
Append(" |> ", Color.Goldenrod, ref _logFileBuilder);
7575
});
7676
}
7777

7878
var (color, value) = VerifySeverity(s);
7979
Append($"{value}:".P(), color);
8080
var dt = DateTime.Now.ToLocalTime();
81-
content.Append($"[{dt.FormatDate()} | {dt.FormatFullTime()}] {value} -> ");
81+
_logFileBuilder.Append($"[{dt.FormatDate()} | {dt.FormatFullTime()}] {value} - ");
8282

8383
(color, value) = VerifySource(src);
8484
Append($"[{value}]".P(), color);
85-
content.Append(string.Intern($"{value} -> "));
85+
_logFileBuilder.Append(string.Intern($"{value} -> "));
8686

8787
if (!message.IsNullOrWhitespace())
88-
Append(message, Color.White, ref content);
88+
Append(message, Color.White, ref _logFileBuilder);
8989

9090
if (e != null)
9191
{
9292
e.SentryCapture(scope =>
9393
scope.AddBreadcrumb("This exception might not have been thrown, and may not be important; it is merely being logged.")
9494
);
9595

96-
Append(errorString(), Color.IndianRed, ref content);
96+
Append(errorString(), Color.IndianRed, ref _logFileBuilder);
9797

9898
string errorString()
9999
=> Environment.NewLine + (e.Message.IsNullOrEmpty() ? "No message provided" : e.Message) +
100100
Environment.NewLine + e.StackTrace;
101101
}
102102

103-
if (Environment.NewLine != content[^1].ToString())
103+
if (Environment.NewLine != _logFileBuilder[^1].ToString())
104104
{
105105
Console.Write(Environment.NewLine);
106-
content.AppendLine();
106+
_logFileBuilder.AppendLine();
107107
}
108-
108+
109109
if (Config.EnabledFeatures?.LogToFile ?? false)
110-
GetLogFilePath(DateTime.Now).AppendAllText(content.ToString());
110+
GetLogFilePath(dt).AppendAllText(_logFileBuilder.ToString());
111+
112+
_logFileBuilder.Length = 0;
111113
}
112114

113115
public static FilePath GetLogFilePath(DateTime date)
@@ -129,8 +131,7 @@ private static void Append(string m, Color c, ref StringBuilder sb)
129131
private static (Color Color, string Source) VerifySource(LogSource source) =>
130132
source switch
131133
{
132-
LogSource.Discord => (Color.RoyalBlue, "DISCORD"),
133-
LogSource.Gateway => (Color.RoyalBlue, "DISCORD"),
134+
LogSource.Discord or LogSource.Gateway => (Color.RoyalBlue, "DISCORD"),
134135
LogSource.Volte => (Color.LawnGreen, "CORE"),
135136
LogSource.Service => (Color.Gold, "SERVICE"),
136137
LogSource.Module => (Color.LimeGreen, "MODULE"),

0 commit comments

Comments
 (0)