4

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?

pnuts
  • 58,317
  • 11
  • 87
  • 139
uhu
  • 1,702
  • 5
  • 17
  • 26
  • 2
    You can try Log4net or Microsoft Logging Application Block. – Asif Mushtaq May 22 '12 at 16:37
  • I personally used log4net wrapped with my own specific code that reduces the number of lines it takes to log a message. My code looks up the logged in user, and adds some other common info that make sense for my application. With some careful setup work, you don't lose any of log4net's fine-grained control over logging. – Katie Kilian May 22 '12 at 17:08

4 Answers4

6

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.

Aaron Deming
  • 1,015
  • 10
  • 12
2

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.

Daniel Mošmondor
  • 19,718
  • 12
  • 58
  • 99
  • That is not obvious at all. In fact, OP says logging is needed "in some dialogs", implying that the requirement isn't to log all changes -- only ones in specific dialogs. It is going to be much harder to install logging at the database level than integrating a framework like log4net. – Katie Kilian May 22 '12 at 17:06
  • I am absolutely sure that it will be harder, but logged data should go into database for reviewers to see anyway. – Daniel Mošmondor May 22 '12 at 17:18
  • Logged data should go into the database? Sure. Logging should be done at the table level? That's the part I disagree with. Logging should be done where it makes sense in the application. – Katie Kilian May 22 '12 at 18:56
  • Please re-read OPs question. How do you plan he distribute log data to audit department better than through the familiar application, thus reading data from the actual database itself. – Daniel Mošmondor May 22 '12 at 20:23
  • I'm not sure you understand what I'm saying. Sure, store the logs wherever it is convenient -- I agree with you that the database makes sense for this. But that doesn't necessarily mean that you should perform the logging at the data layer with your database engine. Logging frameworks such as log4net integrated into the application itself may be more useful, depending on the app. It will allow you to log what the user was doing, what screen the user was on, etc., instead of just logging what was changed in the database. – Katie Kilian May 22 '12 at 20:30
  • Yes, you are 100% right. But doesn't it smell to you like that they want to log CHANGES to the data? So they could have whole history for the document, log of changes to it, and so on... – Daniel Mošmondor May 22 '12 at 21:22
1

A lot of people ignore the fact that .NET has a pretty powerful logging system built in using the Trace class. The nice part is you can use it immediately without any setup, get messages in your debug window, and when you actually need log files, you can set up Trace Listeners.

w.brian
  • 16,296
  • 14
  • 69
  • 118
0

When you want to log datachanges (on a database) you should use the log functionality offered by the database (with stored procedure, trigger and depending the database product built-in functionality like SQL Server Change Data Capture) because most of time you want to log the event regardless of the application/process (Winforms application, Website, Database management software) which trigger it.

You don't want (or will forgive) to recreate your log functionality on the next new client application.

Use the application log system when you want to trace event about the application use (crash, click on a button, start time,...)

Giving access to the log and make it human readable is another story.

Marco Guignard
  • 613
  • 3
  • 9