Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging in a Winform application

Initial situation: There is a big Winform application with many dialogs and an Oracle database in the background. Now there is the requirement to implement a audit logging feature, which logs data changes (before/after) by the users (for later audits by audit departments of the company) in some dialogs. How would you integrate such a logging feature? By the way, the log-information should be saved in the database (history table) and the admin application of the Winform-solution should provide a browser dialog for the logging data.

Are there existing solutions or frameworks, which can be used. Makes it sense to use a logging framework like NLOG in that case or is it better to implement such a specific logging from the scratch?

like image 608
uhu Avatar asked Jan 16 '23 14:01

uhu


2 Answers

I created a pretty simple static class called Logger, that simply has a method that takes a string and logs the current DateTime with a StreamWriter. I like writing my own logs because it allows me to format the output how I want. Here is a short example of what mine looks like :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace LoggerSpace
{
    public static class Logger
    {
        private static StreamWriter swLog;
        private const string sLOG_FILE_PATH = "log.txt";    

        static Logger()
        {
            Logger.OpenLogger();
        }

        public static void OpenLogger()
        {
            Logger.swLog = new StreamWriter(sLOG_FILE_PATH, false);
            Logger.swLog.AutoFlush = true;
        }

        public static void LogThisLine(string sLogLine)
        {
            Logger.swLog.WriteLine(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString() + "\t:" + "\t" + sLogLine);
            Logger.swLog.Flush();
        }

        public static void CloseLogger()
        {
            Logger.swLog.Flush();
            Logger.swLog.Close();
        }
    }
}

You have to make sure to catch appropriate exceptions, and call the close method when your form closes. Again, I like it because it is simple and I can format it how I like it. I also have seen it where people write it where whitespace is generated from certain keywords in the line they log. Just a suggestion, there are so many options.

like image 176
Aaron Deming Avatar answered Jan 25 '23 10:01

Aaron Deming


You have several options for that, and none include system-level logging that some are suggesting.

Options:

  • if you have stored procedures on the database that act as an interface for CRUD operations, you are in luck since you can add logging there
  • if you don't have stored procedures as an interface to the database, you can still avoid recoding the application and use triggers on the tables that are of interest to you
  • last option would be to modify application code and insert logging into the application code itself.

Every option has its advantages and disadvantages, so try to learn as much as you can before leaping in any direction.

EDIT:

WHY you don't need Nlog or log4net

You don't need them because from the question it's obvious that you need the data about executed transactions in the database. Of course that both logging frameworks will be able to put the data into the database, but there will be many extra steps involved to first format the data for the database, then from formatted data extract useful information about entities that were involved in the transaction and so on.

like image 26
Daniel Mošmondor Avatar answered Jan 25 '23 10:01

Daniel Mošmondor