mirror of
https://github.com/AssetRipper/AssetRipper.git
synced 2025-12-11 20:15:29 +01:00
Timestamped Log Files (#1826)
* Enhance logging functionality by implementing log file rotation and timestamped log file naming. * moved log-file naming & rotation to WebApplicationLauncher. * simplify rotation logic
This commit is contained in:
parent
ccc5832761
commit
86e6102640
@ -9,6 +9,7 @@ using AssetRipper.GUI.Web.Pages.Scenes;
|
||||
using AssetRipper.GUI.Web.Pages.Settings;
|
||||
using AssetRipper.GUI.Web.Paths;
|
||||
using AssetRipper.Import.Logging;
|
||||
using AssetRipper.Import.Utils;
|
||||
using AssetRipper.Web.Extensions;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
@ -75,7 +76,12 @@ public static class WebApplicationLauncher
|
||||
|
||||
if (log)
|
||||
{
|
||||
Logger.Add(string.IsNullOrEmpty(logPath) ? new FileLogger() : new FileLogger(logPath));
|
||||
if (string.IsNullOrEmpty(logPath))
|
||||
{
|
||||
logPath = ExecutingDirectory.Combine($"AssetRipper_{DateTime.Now:yyyyMMdd_HHmmss}.log");
|
||||
RotateLogs(logPath);
|
||||
}
|
||||
Logger.Add(new FileLogger(logPath));
|
||||
}
|
||||
Logger.LogSystemInformation("AssetRipper");
|
||||
Logger.Add(new ConsoleLogger());
|
||||
@ -384,4 +390,31 @@ public static class WebApplicationLauncher
|
||||
Logger.Error($"Failed to launch web browser for: {url}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static void RotateLogs(string path)
|
||||
{
|
||||
const int MaxLogFiles = 5;
|
||||
string? directory = Path.GetDirectoryName(path);
|
||||
if (directory is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
FileInfo[] logFiles = new DirectoryInfo(directory)
|
||||
.GetFiles("AssetRipper_*.log")
|
||||
.OrderBy(f => f.Name)
|
||||
.ToArray();
|
||||
|
||||
for (int i = 0; i <= logFiles.Length - MaxLogFiles; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
logFiles[i].Delete();
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
// Could not delete log file, ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user