Here is an introductory article: http://www.codeproject.com/KB/trace/Log4Net_with_ASP_NET.aspx
I think a useful way to track users is to log users by their IP Address. Once your log4net is installed correctly, the following code in the Page_Load event can be used to write IP Addresses to a file placed in the App_Code folder (where it is not public):
if (!Page.IsPostBack)
{
string nowip;
nowip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (nowip == null)
{
nowip = Request.ServerVariables["REMOTE_ADDR"];
}
log.Info("Visitor IP: " + nowip);
}
I'll include the BasePage for a reference to the log variable above (which the System.Web.Page inherits from):
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using log4net;
public class BasePage : System.Web.UI.Page
{
public static readonly ILog log = LogManager.GetLogger("Logger");
public BasePage()
{
initLog4Net();
//logAll();
}
/// <summary>
/// initializes Log4Net
/// Described at http://www.codeproject.com/KB/trace/Log4Net_with_ASP_NET.aspx
/// configured in web.config
/// level value can be ALL ERROR DEBUG FATAL DEBUG OFF WARN
/// </summary>
private void initLog4Net()
{
log4net.Config.XmlConfigurator.Configure();
}
/// <summary>
/// test log4net
/// </summary>
protected void logAllTest()
{
bool isDebugEnabled = log.IsDebugEnabled;
bool isErrorEnabled = log.IsErrorEnabled;
bool isFatalEnabled = log.IsFatalEnabled;
bool isInfoEnabled = log.IsInfoEnabled;
bool isWarnEnabled = log.IsWarnEnabled;
log.Debug("Debug msg");
log.Error("Error msg");
log.Fatal("Fatal msg");
log.Info("Info msg");
log.Warn("Warn msg");
}
}
References: http://www.aspxcode.net/free-asp-net-sample-source-code-c.aspx?Topics=how%20to%20get%20the%20IP%20Address%20of%20the%20Visitor