Repository navigation
Expand file tree
/
Copy pathLogs.cs
More file actions
52 lines (44 loc) · 1.95 KB
/
Copy pathLogs.cs
File metadata and controls
52 lines (44 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace New1
{
internal class Logs
{
public static void Registrar(int idUsuario, string nomeUsuario, string acao, string detalhes)
{
try
{
// Diretório onde os logs serão salvos
string pastaLogs = @"C:\Users\user\Documents\Visual Studio 2022\Daniel\New1\Logs";
// Verifica se a pasta 'Logs' existe, se não, cria
if (!Directory.Exists(pastaLogs))
{
Directory.CreateDirectory(pastaLogs); // Cria a pasta 'Logs' se não existir
}
// Define o nome do arquivo de log com base na data atual (ex: log_2025-05-22.txt)
string dataAtual = DateTime.Now.ToString("yyyy-MM-dd"); // Formato: 2025-05-22
string nomeArquivo = Path.Combine(pastaLogs, $"logs_{dataAtual}.txt");
// Monta a linha do log com a data e hora
string log = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] - Ação: {acao} - Vendedor: {nomeUsuario} (ID: {idUsuario}) - Detalhes: {detalhes}";
// Abre o arquivo de log e adiciona a nova linha
using (StreamWriter writer = new StreamWriter(nomeArquivo, true)) // 'true' para adicionar no final do arquivo
{
writer.WriteLine(log);
}
// Caso o log seja salvo com sucesso, você pode adicionar uma mensagem de confirmação
Console.WriteLine("Log registrado com sucesso!");
}
catch (Exception ex)
{
// Caso ocorra um erro ao gravar o log, exibe a mensagem de erro
MessageBox.Show("Erro ao registrar log no arquivo: " + ex.Message);
}
}
}
}