Iniciar sesión en una aplicación Winform

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 (antes después) 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?

preguntado el 22 de mayo de 12 a las 17:05

You can try Log4net or Microsoft Logging Application Block. -

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

4 Respuestas

I created a pretty simple static class called Logger, that simply has a method that takes a string and logs the current DateTime con un 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()

        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);

        public static void CloseLogger()

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.

contestado el 22 de mayo de 12 a las 17:05

I did such thing before I was introduced to nLog. Look into it, it's simple enough and have all the features you need (formatting). - Daniel Mosmondor

Simple and efficacious! Good idea ;) - vito gentil

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


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


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.

contestado el 22 de mayo de 12 a las 17:05

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

I am absolutely sure that it will be harder, but logged data should go into database for reviewers to see anyway. - Daniel Mosmondor

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

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 Mosmondor

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

A lot of people ignore the fact that .NET has a pretty powerful logging system built in using the Clase de seguimiento. 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 Oyentes de seguimiento.

contestado el 22 de mayo de 12 a las 17:05

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.

Respondido el 26 de junio de 15 a las 17:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.