43 lines
1.3 KiB
C#
43 lines
1.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
|
|
namespace FATrace.OEMApp.Utils
|
|
{
|
|
public static class LogReader
|
|
{
|
|
public static IEnumerable<string> ReadTailLines(string filePath, int lineCount)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(filePath) || !File.Exists(filePath))
|
|
return Enumerable.Empty<string>();
|
|
|
|
var lines = new LinkedList<string>();
|
|
try
|
|
{
|
|
using var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
|
using var sr = new StreamReader(fs);
|
|
string? line;
|
|
while ((line = sr.ReadLine()) != null)
|
|
{
|
|
lines.AddLast(line);
|
|
if (lines.Count > lineCount) lines.RemoveFirst();
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
return Enumerable.Empty<string>();
|
|
}
|
|
return lines;
|
|
}
|
|
|
|
public static string GetTodayLogPath()
|
|
{
|
|
var baseDir = AppContext.BaseDirectory;
|
|
var logDir = Path.Combine(baseDir, "logs");
|
|
var file = Path.Combine(logDir, DateTime.Now.ToString("yyyy-MM-dd") + ".log");
|
|
return file;
|
|
}
|
|
}
|
|
}
|