52 lines
1.4 KiB
C#
52 lines
1.4 KiB
C#
namespace GhostImageManager.Services;
|
|
|
|
public class LogService
|
|
{
|
|
private readonly IWebHostEnvironment _env;
|
|
private readonly string _logsDir;
|
|
|
|
public LogService(IWebHostEnvironment env)
|
|
{
|
|
_env = env;
|
|
_logsDir = Path.Combine(env.ContentRootPath, "logs");
|
|
}
|
|
|
|
public List<string> GetLogFiles()
|
|
{
|
|
if (!Directory.Exists(_logsDir))
|
|
return new List<string>();
|
|
|
|
return Directory.GetFiles(_logsDir, "ghost-image-manager-*.log")
|
|
.OrderByDescending(f => f)
|
|
.ToList();
|
|
}
|
|
|
|
public string ReadLogFile(string fileName, int maxLines = 1000)
|
|
{
|
|
var filePath = Path.Combine(_logsDir, fileName);
|
|
if (!File.Exists(filePath))
|
|
return $"Log-Datei nicht gefunden: {fileName}";
|
|
|
|
try
|
|
{
|
|
var lines = File.ReadAllLines(filePath);
|
|
if (lines.Length <= maxLines)
|
|
return string.Join(Environment.NewLine, lines);
|
|
|
|
return string.Join(Environment.NewLine, lines.Skip(lines.Length - maxLines));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return $"Fehler beim Lesen der Log-Datei: {ex.Message}";
|
|
}
|
|
}
|
|
|
|
public string ReadLatestLog(int maxLines = 1000)
|
|
{
|
|
var files = GetLogFiles();
|
|
if (!files.Any())
|
|
return "Keine Log-Dateien vorhanden.";
|
|
|
|
return ReadLogFile(Path.GetFileName(files.First()), maxLines);
|
|
}
|
|
}
|