CAMERA V1
This commit is contained in:
42
FATrace.OEMApp/Utils/LogReader.cs
Normal file
42
FATrace.OEMApp/Utils/LogReader.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user