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(); | |
} | |
} |
using System;
using System.IO;
class Program
{
static void Main()
{
string arquivoLog = "caminho/do/arquivo.log";
// Abre o arquivo com a opção FileShare.ReadWrite
using (FileStream fileStream = new FileStream(arquivoLog, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (StreamReader reader = new StreamReader(fileStream))
{
string linha;
while ((linha = reader.ReadLine()) != null)
{
// Processa cada linha do arquivo de log
Console.WriteLine(linha);
}
}
}
}
}
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";
// Caminho para a pasta dos logs
string logFolderPath = "resources\\";
// Caminho completo do arquivo de log
string logFilePath = Path.Combine(logFolderPath, logFileName);
try
{
string ultimaLinha = null;
string penultimaLinha = null;
using (StreamReader reader = new StreamReader(logFilePath))
{
string linha;
while ((linha = reader.ReadLine()) != null)
{
penultimaLinha = ultimaLinha;
ultimaLinha = linha;
}
}
// Imprime as duas últimas linhas da cópia virtual
if (penultimaLinha != null)
Console.WriteLine("Penúltima linha da cópia virtual: " + penultimaLinha);
if (ultimaLinha != null)
Console.WriteLine("Última linha da cópia virtual: " + ultimaLinha);
}
catch (Exception ex)
{
Console.WriteLine("Erro ao copiar o arquivo: " + ex.Message);
}
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
string logFileName = string.Format("{0:yyyy-MM-dd} {1}hs.txt", now, now.Hour);