ldj/myLog/RequestLog.cs

157 lines
4.4 KiB
C#
Raw Normal View History

2023-05-23 16:13:17 +08:00
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("浏览器基本信息:<br>");
strLabel.Append("Type = " + bc.Type + "<br>");
strLabel.Append("Name = " + bc.Browser + "<br>");
strLabel.Append("Version = " + bc.Version + "<br>");
strLabel.Append("Platform = " + bc.Platform + "<br>");
strLabel.Append("UserAgent = " + HttpContext.Current.Request.UserAgent + "<br>");
strLabel.Append("UserLanguages = " + HttpContext.Current.Request.UserLanguages[0].ToString() + "<br>");
 
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 += "<Split>" + ((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 <br/> " + er.Source);
// error.Append("StackTrace is <br/>" + er.StackTrace);
if (er.InnerException != null)
error.Append("InnerException is <br/>" + 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)
{
}
}
}
}