开发者

Tracking user-actions in a ASP.net data access application

开发者 https://www.devze.com 2023-01-08 09:29 出处:网络
I need to log every 开发者_StackOverflow中文版user-action taken on a database using the username of the person logged into my ASP.net application as the identifier in the log.

I need to log every 开发者_StackOverflow中文版user-action taken on a database using the username of the person logged into my ASP.net application as the identifier in the log.

What is the easiest way to do this?

I guess that for every method call to my data access layer I need to log the parameters of that call and the username and then call a new "log event" method that writes the details of the call to a table.

Am I on the right track?


There are three obvious choices: use triggers, use SQL to log from your DAL, or use log4net to log from your DAL.

Using Triggers

If you're using triggers, add a LastUser column to every table and make said column required (ie NOT NULL with a check constraint that it's not an empty string). You'll need the LastUser column available to the code in your triggers so you can record who did what. You don't say what database system you're using; some don't support triggers or don't support SELECT triggers (do you need to record SELECT statements being run?).

Log from DAL Using SQL

If you're logging from your DAL into the database, hopefully all your calls go through some central piece of code. If so, then you can add your single logging method in that section of code. Remember that you will want to wrap all calls to the database in a transaction to make sure your log and the actual data don't get out of sync.

Log from DAL Using Log4Net

You could just as easily set up log4net to record all data access. Log4net has the ability to record log entries into log files, but also into databases. Adding log4net to existing code is easy, and configuration is relatively easy.


Well the here where I am working we are doing logging at the database layer. Whenever a user does an insert, update, delete operation a trigger is triggered and the operation with the corresponding fields is logged, for history and auditing. Probably not the best way but it works.

0

精彩评论

暂无评论...
验证码 换一张
取 消