Skip to content

Instantly share code, notes, and snippets.

@sidneyspe
Last active May 22, 2023 13:09
Show Gist options
  • Save sidneyspe/a1ac70521e1c11141ebdd7eae40afd92 to your computer and use it in GitHub Desktop.
Save sidneyspe/a1ac70521e1c11141ebdd7eae40afd92 to your computer and use it in GitHub Desktop.
get the last line of log file
using System;
using System.IO;
class Program
{
static void Main()
{
// Obter a data e hora atual
DateTime now = DateTime.Now;
// Formatar o nome do arquivo de log com base na data e hora atual
string logFileName = now.ToString("yyyy-MM-dd HH'hs'") + ".txt";
Console.WriteLine("🚀 ~ logFileName: -> " + logFileName);
// Caminho para a pasta dos logs
string logFolderPath = "resources\\";
// Caminho completo do arquivo de log
string logFilePath = Path.Combine(logFolderPath, logFileName);
Console.WriteLine("🚀 ~ logFilePath: -> " + logFilePath);
// Verificar se o arquivo de log existe
if (File.Exists(logFilePath))
{
// Ler todas as linhas do arquivo de log
string[] logLines = File.ReadAllLines(logFilePath);
// Verificar se há pelo menos duas linhas no arquivo
if (logLines.Length >= 2)
{
// Última linha do log
string lastLine = logLines[logLines.Length - 1];
// Penúltima linha do log
string secondToLastLine = logLines[logLines.Length - 2];
// Exibir as linhas extraídas
Console.WriteLine(secondToLastLine);
Console.WriteLine(lastLine);
}
else
{
Console.WriteLine("O arquivo de log não contém linhas suficientes.");
}
}
else
{
Console.WriteLine("O arquivo de log não existe.");
}
Console.ReadLine();
}
}
@sidneyspe
Copy link
Author

@sidneyspe
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment