66 lines
2.3 KiB
C#
66 lines
2.3 KiB
C#
using System.IO;
|
|
|
|
namespace UtopiaCanteenSystem.Services.Logging;
|
|
|
|
/// <summary>
|
|
/// File log directories. Client uses the current user's LocalAppData.
|
|
/// Backend (Windows Service) uses the interactive user's LocalAppData when possible
|
|
/// so logs appear under the logged-on user's profile instead of systemprofile.
|
|
/// </summary>
|
|
public static class LogPaths
|
|
{
|
|
private static string? _backendLogsDirectoryOverride;
|
|
private static string? _cachedLocalAppDataRoot;
|
|
|
|
/// <summary>
|
|
/// Optional absolute path (e.g. from appsettings LogsDirectory). Used by the backend service only.
|
|
/// </summary>
|
|
public static void SetBackendLogsDirectory(string? directory)
|
|
{
|
|
_backendLogsDirectoryOverride = string.IsNullOrWhiteSpace(directory)
|
|
? null
|
|
: directory.Trim();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Client logs folder: %LocalAppData%\UtopiaCanteenClient\Logs.
|
|
/// </summary>
|
|
public static string ClientLogsDirectory =>
|
|
Path.Combine(ResolveLocalAppDataRoot(), "UtopiaCanteenClient", "Logs");
|
|
|
|
/// <summary>
|
|
/// Backend logs folder: %LocalAppData%\UtopiaCanteenBackend\Logs
|
|
/// (interactive user when running as a service).
|
|
/// </summary>
|
|
public static string BackendLogsDirectory =>
|
|
_backendLogsDirectoryOverride ?? Path.Combine(ResolveLocalAppDataRoot(), "UtopiaCanteenBackend", "Logs");
|
|
|
|
public static string GetBackendLogFilePath(DateTime? date = null)
|
|
{
|
|
var d = date ?? DateTime.Now;
|
|
return Path.Combine(BackendLogsDirectory, $"backend-{d:yyyy-MM-dd}.log");
|
|
}
|
|
|
|
public static string GetClientLogFilePath(DateTime? date = null)
|
|
{
|
|
var d = date ?? DateTime.Now;
|
|
return Path.Combine(ClientLogsDirectory, $"client-{d:yyyy-MM-dd}.log");
|
|
}
|
|
|
|
/// <summary>
|
|
/// User's LocalAppData, or SYSTEM profile when no interactive session (service at login screen).
|
|
/// </summary>
|
|
public static string ResolveLocalAppDataRoot()
|
|
{
|
|
if (!string.IsNullOrEmpty(_cachedLocalAppDataRoot))
|
|
return _cachedLocalAppDataRoot;
|
|
|
|
var interactive = InteractiveUserPath.TryGetLocalAppDataPath();
|
|
if (!string.IsNullOrWhiteSpace(interactive))
|
|
return _cachedLocalAppDataRoot = interactive;
|
|
|
|
return _cachedLocalAppDataRoot =
|
|
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
|
|
}
|
|
}
|