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(); | |
| } | |
| } |
Author
Author
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);
}
}
}
}
}
Author
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);