- ghost admin
This commit is contained in:
parent
6e558d90d0
commit
00d2681a70
1 changed files with 52 additions and 0 deletions
52
GhostImageManager/Services/LogService.cs
Normal file
52
GhostImageManager/Services/LogService.cs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue