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?
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.