Click here to Skip to main content
16,020,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I was wondering if someone can show me some good references in logging user activity in a winform application. I would like to log when the user logs in, when the user does certain tasks, submits data into the database, etc...

I would like to log the activity either in SQL database or just in plain text file. If anyone has any good references or examples to look at, I would greatly appreciate it.
Posted
Updated 30-Dec-20 5:13am
Comments
[no name] 3-Jul-14 16:46pm    
research "logging" and log4net.
Sergey Alexandrovich Kryukov 3-Jul-14 16:53pm    
Didn't you forget FCL EventLog..? :-)
I answered, please see. Your advice is credited, of course.
—SA
Sergey Alexandrovich Kryukov 3-Jul-14 16:47pm    
Are you sure the whole idea of spying on the user is good? Maybe, the reason is different and legitimate; I'm just checking. I used to work on one well-known organization where creation of any software monitoring users or employees activity is formally and strictly prohibited by the company policy, by legal and ethical reasons...
—SA
Member 10622146 3-Jul-14 16:53pm    
Im trying to log a certain functionality of the application which is critical per user. Its highly dependent of users to do daily tasks which require different levels.
Sergey Alexandrovich Kryukov 3-Jul-14 16:54pm    
Not clear. Is it spying on user activity or not?
I answered your question, by the way. Your follow-up questions will be welcome.
—SA

First of all, you should put UI code in proper order: instead of handling all those buttons and text boxes on ad-hoc basis normally offered by the Designer. Instead, you need to abstract out the set of centralized actions which are called on raw control events, such as button click, text change (by the way, not so easy in System.Windows.Forms, unlike some other libraries; you will have to handle raw mouse/keyboard events and then see if the text is changed), selection changes, and so on. This way, for example, if the same logical action is invoked when you click on a button or hit a default key, will be handled by the same action.

When this is done, you can (optionally) add logging to each of those actions. One ready-to-use logging tool is System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog%28v=vs.110%29.aspx[^].

It writes the logs to the system event log, but the sink of the log data can be customized, as well as nearly all other relevant detail. Please see my past answers:
MsBuild OutPut to the TextBox on the fly in Windows Application[^],
How to create event log under a folder[^].

And yes, as Wes Aday recommended, there is another option, log4net:
http://en.wikipedia.org/wiki/Log4net#Ports[^],
http://logging.apache.org/log4net[^].

—SA
 
Share this answer
 
Comments
Espen Harlinn 3-Jul-14 18:45pm    
5'ed!
Sergey Alexandrovich Kryukov 3-Jul-14 20:47pm    
Thank you, Espen.
—SA
Logging user activity is an important part of auditing, and is necessary for regulatory compliance in many situations. I use the MS Enterprise Library Logging Application Block where possible:

http://msdn.microsoft.com/en-us/library/dn440731%28v=pandp.60%29.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900