Ensure that log file names do not use invalid filename characters.

This commit is contained in:
Michael VanOverbeek 2021-11-02 21:50:01 -04:00 committed by jvyden
parent 455341ffeb
commit 97fe7b07cd
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
2 changed files with 22 additions and 1 deletions

View file

@ -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;
}
}
}

View file

@ -2,6 +2,7 @@ using System;
using System.IO; using System.IO;
using Kettu; using Kettu;
using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Helpers.Extensions;
namespace LBPUnion.ProjectLighthouse.Logging namespace LBPUnion.ProjectLighthouse.Logging
{ {
@ -18,7 +19,7 @@ namespace LBPUnion.ProjectLighthouse.Logging
string contentFile = $"{channel}{line.LineData}\n"; string contentFile = $"{channel}{line.LineData}\n";
string contentAll = $"[{$"{line.LoggerLevel.Name} {channel}".TrimEnd()}] {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); File.AppendAllText(Path.Combine(logsDirectory, "all.log"), contentAll);
} }
public override bool AllowMultiple => false; public override bool AllowMultiple => false;