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 System.Data.Common;

public class LoggingModule : IHttpModule {
    public void Init(HttpApplication Application) {
        Application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
    }

    public void Application_BeginRequest(Object sender, EventArgs e) {
        HttpContext ctx = ((HttpApplication)sender).Context;
        ConnectionStringSettings setting = 
          ConfigurationManager.ConnectionStrings["MyDB"];
        DbProviderFactory factory = 
          DbProviderFactories.GetFactory(setting.ProviderName);
        using(DbConnection db = factory.CreateConnection()) {
            db.ConnectionString = setting.ConnectionString;
            DbCommand comm = factory.CreateCommand();
            comm.CommandText = "INSERT INTO LogInfo(url,userAgent,accessTime) VALUES(@url,@userAgent,@accessTime)";
            comm.Connection = db;
            DbParameter pUrl = factory.CreateParameter();
            pUrl.ParameterName = "@url";
            pUrl.Value = ctx.Request.Url.ToString();
            comm.Parameters.Add(pUrl);
            DbParameter pAgent = factory.CreateParameter();
            pAgent.ParameterName = "@userAgent";
            pAgent.Value = ctx.Request.UserAgent;
            comm.Parameters.Add(pAgent);
            DbParameter pAccess = factory.CreateParameter();
            pAccess.ParameterName = "@accessTime";
            pAccess.Value = DateTime.Now.ToString();
            comm.Parameters.Add(pAccess);
            db.Open();
            comm.ExecuteNonQuery();
        }
    }

    public void Dispose() {
    }
}
