Last active
May 22, 2023 13:09
-
-
Save sidneyspe/a1ac70521e1c11141ebdd7eae40afd92 to your computer and use it in GitHub Desktop.
get the last line of log file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
using System;
using System.IO;
class Program
{
static void Main()
{
// Obter a data e hora atual
DateTime now = DateTime.Now;
}
}