ASP.NET 日志的记录(登录日志和异常日志和操作日志)

一、日志的记录

       日志的记录日志的记录其实没那么简单,需要登录日志,异常日志。其实可以有这句话引出

    try
        {
            LogHelper.Insert("fun_orgmgr", "机构管理", "新增了机构:" + orgname, 1, UserAuthentication.Current().GetUserAuthInfo().UserID);
        }
        catch (Exception ex)
        {
            result.Code = "1";
            result.Message = ex.Message;
            LogHelper.AddError("fun_orgmgr", "删除机构信息时出现异常:" + ex.Message, 1, ex.StackTrace);
        }
日志记录页面代码
       其中的LogHelper.AddError()是Controls层中的LogHelper类的AddError方法,是增加日志功能。LogHelper.Insert()是增加日志功能,这两个日志的增加要放到不同的表里面。例如T_LoginLog和T_LoginLog

 在Insert()中的那几个字段是干什么用的呢?功能ID,日志摘要,日志正文,日志类型,发起用户ID,可以调出LogHelper.Insert()方法来看。

       看前注意,这个LogHelper类是放到Controls层里面的,专门用来解决日志的问题,按我的理解应该是个bll层,拿AddError()来说,在赋值实体之前,实例化了一个实体,这个没什么说的,又实例化一个ActionLogService类,这个类是bll层的对日志实体进行增删改的操作,不仅对流程熟悉下,对怎样插入日志的需要的字段理清下。最后用return bll.Insert(entity);来插入日志。

namespace XTSoft.Com.Controls
{
    public class LogHelper
    {
        public LogHelper()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }



        /// 
        /// 客户端IP
        /// 
        /// 
        private static string ClientIP()
        {
            string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (null == result || result == String.Empty)
            {
                result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }

            if (null == result || result == String.Empty)
            {
                result = HttpContext.Current.Request.UserHostAddress;
            }
            return result;
        }

        /// 
        /// 插入登录日志
        /// 
        /// 日志摘要
        /// 日志正文
        /// 日志类型1=浏览器登录,2=客户端登录
        /// 发起用户ID
        /// 
        public static int Insert(string Summary, string Describe, int LoginType, Guid UserID)
        {
            LoginLogService bll = new LoginLogService();
            HttpBrowserCapabilities b = HttpContext.Current.Request.Browser;
            T_LoginLog entity = new T_LoginLog();
            entity.Summary = Summary;
            entity.Describe = Describe;
            entity.LoginType = LoginType;
            entity.UserID = UserID;
            entity.CreateDate = DateTime.Now;
            entity.ClientIP = ClientIP();
            entity.UserAgent = b.Browser + " " + b.Version;
            entity.RequestUrl = HttpContext.Current.Request.Url.AbsolutePath;
            entity.RefererUrl = HttpContext.Current.Request.UrlReferrer != null ? HttpContext.Current.Request.UrlReferrer.AbsolutePath : string.Empty;
            return bll.Insert(entity);
        }

        /// 
        /// 插入异常日志
        /// 
        /// 功能ID
        /// 异常摘要
        /// 异常类型1=系统异常,2=自定义
        /// 异常描述
        /// 
        public static int AddError(string FunctionID, string Summary, int ErrorType, string Content)
        {
            int result = 0;
            try
            {
                ErrorLogService bll = new ErrorLogService();
                HttpBrowserCapabilities b = HttpContext.Current.Request.Browser;
                T_ErrorLog entity = new T_ErrorLog();
                entity.FunctionID = FunctionID;
                entity.Summary = Summary;
                entity.ErrorType = ErrorType;
                entity.Content = Content;
                entity.CreateDate = DateTime.Now;
                entity.ClientIP = ClientIP();
                entity.UserAgent = b.Browser + " " + b.Version;
                entity.RequestUrl = HttpContext.Current.Request.Url.AbsolutePath;
                entity.RefererUrl = HttpContext.Current.Request.UrlReferrer.AbsolutePath;
                result= bll.Insert(entity);
            }
            catch { }
            return result;
        }

       /// 
        /// 插入操作日志
       /// 
        /// 功能ID
        /// 日志摘要
        /// 日志正文
        /// 日志类型1=模块访问
        /// 发起用户ID
       /// 
        public static int Insert(string FunctionID, string Summary, string Describe, int LogType, Guid UserID)
        {
            ActionLogService bll = new ActionLogService();
            HttpBrowserCapabilities b = HttpContext.Current.Request.Browser;
            T_ActionLog entity = new T_ActionLog();
            entity.FunctionID = FunctionID;
            entity.Summary = Summary;
            entity.Describe = Describe;
            entity.LogType = LogType;
            entity.UserID = UserID;
            entity.CreateDate = DateTime.Now;
            entity.ClientIP = ClientIP();
            entity.UserAgent = b.Browser + " " + b.Version;
            entity.RequestUrl = HttpContext.Current.Request.Url.AbsolutePath;
            entity.RefererUrl = HttpContext.Current.Request.UrlReferrer.AbsolutePath;
            return bll.Insert(entity);
        }
    }
}
LogHelper.cs
namespace XTSoft.Com.BLL
{
   public class ActionLogService : PagerHelper
    {
        IActionLogProvider dal = new ActionLogProvider();

        public ActionLogService()
        { }

        /// 
        /// 单条插入数据
        /// 
        /// 实体
        /// 
        public int Insert(T_ActionLog entity)
        {
            if (dal.Insert(entity) > 0)
            {
                return 1;
            }
            return -1;
        }

        /// 
        /// 批量插入数据
        /// 
        /// 实体
        /// 
        public int Insert(IList entities)
        {
            if (dal.Insert(entities) > 0)
            {
                return 1;
            }
            return -1;
        }

        /// 
        /// 单条删除数据
        /// 
        /// 
        /// 
        public int Delete(T_ActionLog entity)
        {
            return dal.Delete(entity);
        }

        /// 
        /// 批量删除数据
        /// 
        /// 
        /// 
        public int Delete(IList entities)
        {
            return dal.Delete(entities);
        }
        
        /// 
        /// 单条更新数据(自动插入数据到任务备注记录表)
        /// 
        /// 更改实体
        /// 
        public int Update(T_ActionLog entity)
        {
            return dal.Update(entity);
        }

        /// 
        /// 批量更新数据(自动插入数据到任务备注记录表)
        /// 
        /// 更改实体
        /// 
        public int Update(IList entities)
        {
            return dal.Update(entities);
        }
        
        /// 
        /// 传人DateSet获取实体
        /// 
        /// 
        /// 
        public IList GetRowEntity(DataSet ds)
        {
            return dal.GetRowEntity(ds);
        }


        /// 
        /// 根据id获取结果
        /// 
        /// 查询id
        /// 
        public T_ActionLog selToID(T LogID)
        {
            return dal.selToID(LogID);
        }
    }
}
ActionLogService.cs

     注意到ClientIP()方法,与之前记录过的IP获取的方法是一样的,若客户端真实的IP不存在,那就获取代理IP,若真是的IP存在直接读取。

     http://www.cnblogs.com/d685600/p/3683467.html

二、LogHelper.cs知识点分解

      1、注意到LogHlper.AddError()中有HttpBrowserCapabilities b = HttpContext.Current.Request.Browser; 这是用来获取浏览器的版本信息在entity.UserAgent = b.Browser + " " + b.Version;对实体进行插入,数据库中会显示IE9.0注意这是两个b.Browser和b.Version的结合。

      2、HttpContext.Current.Request.Url.AbsolutePath;HttpContext.Current.Request.UrlReferrer.AbsolutePath;分别是当前请求的URL和请求连接到当前URL的URL。这在日志记录中也是必要的

           entity.RequestUrl = HttpContext.Current.Request.Url.AbsolutePath;

           entity.RefererUrl = HttpContext.Current.Request.UrlReferrer.AbsolutePath;

转载于:https://www.cnblogs.com/d685600/p/3691866.html

你可能感兴趣的:(ASP.NET 日志的记录(登录日志和异常日志和操作日志))