generate a User log in C#

Asked

Viewed 1,321 times

0

Personal my college project I am doing in C# windowsform and would like to generate a logo that when a user include, delete, change in any form generates a log where I can create a log form of all users who do any event.

in each form I created a logged userConnected that brings me from the menu and step forward as parameter I know who is running the form at the moment

I don’t even know where to start.

graduation

  • How’s your data layer? Do you use Entity Framework? If not, you wrote the data layer?

2 answers

1

Create a class, for example Historico.

In it the properties UsuarioAlterou, DataHora, Tela, Observacao.

Create the method Salvar()

Every time you persist some entity, you call this method Salvar()

1

You can use the code below. It is not very complete but already meets the goal.

namespace LogUtils
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Threading;

    public class LogWriter
    {
        // cria um modelo de dados para armazenar as informações de log. Como ele só será usado nesta classe pode ser definido dentro dela como interno.
        internal class LogEntry
        {
            public DateTime Date { get; set; }
            public string UserLogin { get; set; }
            public string Message { get; set; }
        }
        // Singleton, usado para acesso as funcionalidade dessa classe. Garante que só exista uma instancia por aplicação.
        private static LogWriter instance;
        public static LogWriter Instance
        {
            get
            {
                if (instance == null)
                    instance = new LogWriter();
                return instance;
            }
        }
        // final da declaração do singleton

        private Queue<LogEntry> LogPool;
        private Thread thread;
        //Armazena a pasta atual da aplicação
        private string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Replace("file:\\", "");

        public LogWriter()
        {
            // Cria a fila de entradas de log
            LogPool = new Queue<LogEntry>();
            // Cria a thread responsável por gravar log no arquivo  
            thread = new Thread(new ThreadStart(WriteOnDisk));

            thread.Start();
        }
        // Tarefa a ser executada pela thread
        private void WriteOnDisk()
        {
            while (true) // executa infinitamente
            {
                if (LogPool.Count > 0) // verfica se existem logs para gravar
                {
                    LogEntry entry = LogPool.Dequeue(); // retira a entrada de log da fila.
                    //Formata o caminho para o armazenamento do arquivo de log
                    string finalPath = Path.Combine(path, "Logs", entry.Date.Year.ToString(), entry.Date.Month.ToString(), entry.Date.Day.ToString() + ".log");
                    //cria a pasta caso ela não exista.
                    if (!Directory.Exists(Path.GetDirectoryName(finalPath)))
                        Directory.CreateDirectory(Path.GetDirectoryName(finalPath));

                    //grava o log
                    using (StreamWriter sw = File.AppendText(finalPath))
                    {
                        sw.WriteLine(string.Format("{0} - {1} - {2}", entry.Date, entry.UserLogin, entry.Message));
                    }
                }
            }
        }

        public void WriteLog(string userLogin, string message)
        {
            //Cria um objeto do tipo LogEntry
            LogEntry entry = new LogEntry { Date = DateTime.Now, UserLogin = userLogin, Message = message };
            //Adiciona entrada na fila
            LogPool.Enqueue(entry);
        }
    }
}

Copy and paste it into a new.c file. To use it call the function below just after performing your system tasks:

LogUtils.LogWriter.Instance.WriteLog(NOME_USUARIO, MENSAGEM);

Attention, never install this class. Always access it as shown above.

For example:

LogUtils.LogWriter.Instance.WriteLog(login, "O usuário" + login + "alterou o registro);

The class uses a unique thread to manage the log file. This class is suitable for both windows Forms and WPF and ASP.NET projects.

I hope I’ve helped.

If you like the answer, don’t forget to increase its score. If the answer solved your problem, set the question to solved. This helps other people looking for the same theme. Thank you.

  • Note that I am using a thread to record. This creates an isolated task, which can even be processed in another processor core, running in the background in your application.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.