From 97fe7b07cd4bf32305b985a38e972e6bb742c851 Mon Sep 17 00:00:00 2001 From: Michael VanOverbeek Date: Tue, 2 Nov 2021 21:50:01 -0400 Subject: [PATCH] Ensure that log file names do not use invalid filename characters. --- .../Helpers/Extensions/StringExtensions.cs | 20 +++++++++++++++++++ .../Logging/LighthouseFileLogger.cs | 3 ++- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 ProjectLighthouse/Helpers/Extensions/StringExtensions.cs diff --git a/ProjectLighthouse/Helpers/Extensions/StringExtensions.cs b/ProjectLighthouse/Helpers/Extensions/StringExtensions.cs new file mode 100644 index 00000000..4ed898e9 --- /dev/null +++ b/ProjectLighthouse/Helpers/Extensions/StringExtensions.cs @@ -0,0 +1,20 @@ +using System.IO; + +namespace LBPUnion.ProjectLighthouse.Helpers.Extensions +{ + public static class StringExtensions + { + public static string ToFileName(this string text) + { + char[] invalidPathChars = Path.GetInvalidFileNameChars(); + string path = text; + + foreach (char c in invalidPathChars) + { + path = path.Replace(c.ToString(), ""); + } + + return path; + } + } +} \ No newline at end of file diff --git a/ProjectLighthouse/Logging/LighthouseFileLogger.cs b/ProjectLighthouse/Logging/LighthouseFileLogger.cs index 8dbfea54..31879a69 100644 --- a/ProjectLighthouse/Logging/LighthouseFileLogger.cs +++ b/ProjectLighthouse/Logging/LighthouseFileLogger.cs @@ -2,6 +2,7 @@ using System; using System.IO; using Kettu; using LBPUnion.ProjectLighthouse.Helpers; +using LBPUnion.ProjectLighthouse.Helpers.Extensions; namespace LBPUnion.ProjectLighthouse.Logging { @@ -18,7 +19,7 @@ namespace LBPUnion.ProjectLighthouse.Logging string contentFile = $"{channel}{line.LineData}\n"; string contentAll = $"[{$"{line.LoggerLevel.Name} {channel}".TrimEnd()}] {line.LineData}\n"; - File.AppendAllText(Path.Combine(logsDirectory, line.LoggerLevel.Name + ".log"), contentFile); + File.AppendAllText(Path.Combine(logsDirectory, line.LoggerLevel.Name.ToFileName() + ".log"), contentFile); File.AppendAllText(Path.Combine(logsDirectory, "all.log"), contentAll); } public override bool AllowMultiple => false;