using System;
using System.Data;
using System.Text;
using System.Web;
namespace DeiNiu
{
   public  class RequestLog
    {
        private Log _obj;
        public RequestLog()
        {
            _obj = new Log();
             basicLogInfo();
            
           
        }
         StringBuilder  getClientBrowserInfo()
        {
            StringBuilder strLabel = new StringBuilder();
            HttpBrowserCapabilities bc = HttpContext.Current.Request.Browser;
            strLabel.Append("浏览器的分辨率为:");
            strLabel.Append(HttpContext.Current.Request.Form["WidthPixel"]);
           
            strLabel.Append(HttpContext.Current.Request.Form["HeightPixel"]);
            
            strLabel.Append("浏览器基本信息:
");
            strLabel.Append("Type = " + bc.Type + "
");
            strLabel.Append("Name = " + bc.Browser + "
");
            strLabel.Append("Version = " + bc.Version + "
");
            
            strLabel.Append("Platform = " + bc.Platform + "
");
            
            strLabel.Append("UserAgent = " + HttpContext.Current.Request.UserAgent + "
");
            strLabel.Append("UserLanguages = " + HttpContext.Current.Request.UserLanguages[0].ToString() + "
");
       
           
             
            return strLabel;
        }
       
        public   string getIP( )
        {
            string userIP ="";
          
            // 如果使用代理,获取真实IP   
            try
            {  
                HttpRequest Request = HttpContext.Current.Request;
               userIP = Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != "" ? Request.ServerVariables["REMOTE_ADDR"] : Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            
            if (string.IsNullOrEmpty(userIP))
                userIP =  Request.UserHostAddress;
              
            }catch  
            {
                
            }
             return userIP;
        }
       public  void RecordLog(IDbCommand cmd)
        {
           
            
           _obj.log_sql = cmd.CommandText;
           for (int i = 0; i < cmd.Parameters.Count;i++ )
           {
               _obj.log_parameters += "" + ((System.Data.SqlClient.SqlParameter)(cmd.Parameters[i])).Value ;
           }
           _obj.Add();
        }
        public void RequestPageLog()
        {
            _obj.Add();
        }
        public void LoginLog(string account, string logStatus)
        {
            _obj.log_account = account;
            _obj.log_method = logStatus;
            _obj.log_browseinfo = getClientBrowserInfo().ToString();
            _obj.Add();
        }
        public void LogoutLog(int empid)
        {
           
           _obj.logOut(empid);
             
        }
       public void LogoutLog(string userId)
       {
           try{ _obj.logTimeOut(int.Parse(userId));}
           catch(Exception)
           {
               
           }
          
           
       }
        
        public void RecordExceptionLog(Exception er)
        {
            StringBuilder error = new StringBuilder();
            //error.Append("Source is 
 " + er.Source);
           // error.Append("StackTrace is 
" + er.StackTrace);  
            if (er.InnerException != null)
            error.Append("InnerException is 
" + er.InnerException);
            else
            {
                error.Append(er.ToString());
            }
            _obj.log_exception = error.ToString();
           _obj.Add();
          
           
           
        }
        void basicLogInfo()
        {    
            _obj.log_clientip = getIP();
            try
            {
                _obj.log_empid = HttpContext.Current.Session["CurrentUserId"] == null
                                ? -1
                                : Convert.ToInt32(HttpContext.Current.Session["CurrentUserId"].ToString());
           _obj.log_page = HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath + HttpContext.Current.Request.Url.Query;
           _obj.log_sessionid = HttpContext.Current.Session.SessionID;
           _obj.log_account = HttpContext.Current.Session["CurrentUserAccount"] == null
                                ? string.Empty
                                : HttpContext.Current.Session["CurrentUserAccount"].ToString();
        
            }
            catch (Exception er)
            {
            }
     
      
           
        }
    }
}