Login to Winform Application

Initial situation. In the background, there is a large Winform application with many dialog boxes and an Oracle database. Now there is a need to implement the audit logging function , which records data changes ( before / after ) by users (for subsequent audits by the audit departments of the company) in some dialogs. How would you integrate this logging feature? By the way, the log information should be stored in the database (history table), and the Winform solution administrator application should provide a browser dialog for logging data.

Are there existing solutions or frameworks that can be used. Does it make sense to use a registration framework such as NLOG in this case, or is it better to implement such a specific journal from scratch?

+4
source share
4 answers

I created a fairly simple static Logger class that just has a method that takes a string and registers the current DateTime with StreamWriter . I like to write my own journals because it allows me to format the output as I want. Here is a brief 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 should keep an eye on the relevant exceptions and call the close method when your form closes. Again, I like it because it is simple, and I can format it as I like. I also saw this where people write, where spaces are generated from certain keywords in the line that they register. Just a suggestion, there are so many options.

+6
source

You have several options for this, and none of them include the system level registration that some offer.

Parameters:

  • If you store procedures in a database that act as an interface for CRUD operations, you're in luck as you can add a log there
  • If you do not have stored procedures as an interface to the database, you can still not transcode the application and use triggers in the tables of interest to you.
  • The final option would be to change the application code and insert the log into the application code itself.

Each option has its own advantages and disadvantages, so try to learn as much as possible before jumping in any direction.

EDIT:

WHY you don't need Nlog or log4net

You do not need them, because from the question it is obvious that you need data on completed transactions in the database. Of course, both logging structures will be able to put data into the database, but formatting the data for the database will require many additional steps, then useful information about the objects that were involved in the transaction is extracted from the formatted data, and so on.

+2
source

Many people ignore the fact that .NET has a fairly powerful logging system built using the trace class . The nice part is you can use it immediately without any configuration, receive messages in the debug window, and when you really need the log files, you can configure Trace Listeners .

+1
source

If you want to log data (in the database), you must use the log functions offered by the database (with stored procedure, trigger, and depending on the built-in functions of the database product, such as SQL Server Change Data Capture ), because most of the time You want to register an event regardless of the application / process (Winforms application, website, database management software) that trigger it.

You do not want (or forgive) to recreate your journal functions in the next new client application.

Use the application log system if you want to track an application application event (crash, click on a button, start time, ...)

Giving access to the magazine and making it accessible to humans is another story.

0
source

Source: https://habr.com/ru/post/1413803/


All Articles