views:

61

answers:

3

How can we log user operations for a asp.net application. Further what is the approach for saving the log data ? Kindly guide.

+2  A: 

I recommend to use a logging framework like log4net or NLog. These frameworks allow you to log to many destinations and more importantly they allow you to make the decision after you finished your application i.e. you can configure where the log messages are written.

Personally I would log to a database in case of web applications.

Stefan Egli
+1  A: 

or, you could use the Common Logging infrastructure to hide the implementation and switch between loggers at will
http://netcommon.sourceforge.net

Where you store the logging is largely dictated by how you will consume the logging data(i.e. read and interpret the logging data, take action if needed). If the person who analyzes the logging data has access to the machine and it's not business-critical, just log to file. If the machine is critical to your business you probably have some sort of monitoring software and publishing the logging to event logs or WMI becomes interesting.

If you log to file, consider how long you need the logging data and how much of it you need at one time. You can use rolling log files to make sure they don't become gigantically large and consume half your hard disk space. You can also use filters or priorities to log only errors when there is nothing wrong, and open the filter to debug or verbose when investigating a problem.

IIRC log4net and/or enterprise library can log to a format that is readable by the WCF service trace viewer, see http://msdn.microsoft.com/en-us/library/ms732023.aspx (but i'm not sure about that one). Log4net has a dashboard, though.

StephaneT
can you kindly elaborate the "consume the results". thanks.
HotTester
I mean 'read and interpret the logging data, take action if needed'. This can be completely manual, or partly automated if you have a monitoring solution.
StephaneT
+1  A: 

Their is two method either you save the log details into the database or create simple text file so that you can track the events.

Here i am the giving just simple code to how to track the event and maintain the log details and save the log details into the text file . it may help you to solve your problem

first of all you have to import name space

using System.IO;

i had create the small function to track the log details-

enter code here

public void LogEntry(string msg, string path) { try { //It will open the file, append the your message and close the file
File.AppendAllText(path,msg);
} catch (Exception ex) { ex.GetBaseException(); } }

In this function you have to pass the two parameter

  1. Message
  2. Path of your text File

For Example

string logmsg; logmsg = "** " + DateTime.Now.ToString() + "||" + "User:" + UserName + "||" + " EventDesc: Login Attempt Failed"; l1.LogEntry(logmsg, Server.MapPath("LoginEvent.txt"));

here the LoginEvnet.txt is the name of the text file where i am storing the log details

logmsg-It is the message you have to track the or store in the log file

naval