.net 系统操作日志

/// <summary>
    /// 操作日志相关
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class LogManager<T> where T : new()
    {
        static Database db = new Database(DBHelper.GetConnection());

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="operationUser">当前登录用户</param>
        /// <param name="operateType">操作类型</param>
        /// <param name="newMd"></param>
        /// <param name="oldMd">新增操作时,此可为空</param>
        public void AddLog(string operationUser, OperateType operateType, T newMd, T oldMd)
        {
            T_OperateLog log = new T_OperateLog();
            List<LogDetail> logDetailList = new List<LogDetail>();

            //取得m的Type实例
            Type t = newMd.GetType();

            log.OperateUser = operationUser;
            log.OperateType = operateType;
            log.TblName = typeof(T).Name;
            log.CreateDate = DateTime.Now;
            log.TblNameDesc = ((DescriptionAttribute)System.Attribute.GetCustomAttributes(typeof(T))[0]).Description;

            using (var db = new Database(AppConfig.DefaultConnectionString, "SqlServer"))
            {
                int logPKeyVal = 0;
                var logPKey = db.Insert("T_OperateLog", "LogId", log);
                if (logPKey != null)
                {
                    logPKeyVal = Convert.ToInt32(logPKey);
                }
                switch (operateType)
                {
                    case OperateType.Add:
                        AddDelOperation(operateType, t, logPKeyVal, newMd);
                        break;
                    case OperateType.Edit:
                        EditOperation(t, logPKeyVal, oldMd, newMd);
                        break;
                    case OperateType.Del:
                        AddDelOperation(operateType, t, logPKeyVal, newMd);
                        break;
                    case OperateType.Query:
                        //EditOperation(t, logPKeyVal, oldMd, newMd);
                        break;
                }
            }


        }

        /// <summary>
        /// 编辑操作
        /// </summary>
        /// <param name="t"></param>
        /// <param name="logKey"></param>
        /// <param name="oldMd"></param>
        /// <param name="newMd"></param>
        private void EditOperation(Type t, int logKey, T oldMd, T newMd)
        {
            //取得类的属性名并获取属性值和Description
            foreach (PropertyInfo property in t.GetProperties()) //循环遍历
            {
                LogDetail logDetail = new LogDetail();
                string propertyDsc = string.Empty;
                var oldObj = oldMd.GetType().GetProperty(property.Name).GetValue(oldMd, null);
                var oldVal = oldObj == null ? "" : oldObj.ToString();
                var newObj = newMd.GetType().GetProperty(property.Name).GetValue(newMd, null);
                var newVal = newObj == null ? "" : newObj.ToString();
                if (oldVal != newVal)
                {
                    logDetail.LogId = logKey;
                    logDetail.ColumnName = property.Name;
                    logDetail.ColumnDesc = GetCustomerDesc(property);
                    logDetail.OldVal = oldVal;
                    logDetail.NewVal = newVal;
                    logDetail.CreateDate = DateTime.Now;

                    using (var db = new Database(AppConfig.DefaultConnectionString, "SqlServer"))
                    {
                        db.Insert("T_LogDetail", "ID", logDetail);
                    }
                }
            }
        }

        /// <summary>
        /// 新增/删除操作
        /// </summary>
        /// <param name="operateType"></param>
        /// <param name="t"></param>
        /// <param name="logKey"></param>
        /// <param name="newMd"></param>
        private void AddDelOperation(OperateType operateType, Type t, int logKey, T newMd)
        {
            //取得类的属性名并获取属性值和Description
            foreach (PropertyInfo property in t.GetProperties()) //循环遍历
            {
                LogDetail logDetail = new LogDetail();
                string propertyDsc = string.Empty;
                var newObj = newMd.GetType().GetProperty(property.Name).GetValue(newMd, null);
                var newVal = newObj == null ? "" : newObj.ToString();
                logDetail.LogId = logKey;
                logDetail.ColumnName = property.Name;
                logDetail.ColumnDesc = GetCustomerDesc(property);
                logDetail.OldVal = operateType == OperateType.Del ? newVal : "";
                logDetail.NewVal = operateType == OperateType.Add ? newVal : "";
                logDetail.CreateDate = DateTime.Now;

                using (var db = new Database(AppConfig.DefaultConnectionString, "SqlServer"))
                {
                    db.Insert("T_LogDetail", "ID", logDetail);
                }
            }
        }

        /// <summary>
        /// 获取Model的DescriptionAttribute
        /// </summary>
        /// <param name="property"></param>
        /// <returns></returns>
        public string GetCustomerDesc(PropertyInfo property)
        {
            string propertyDsc = string.Empty;
            var arrProDesc = property.GetCustomAttributes(typeof(DescriptionAttribute), true);
            if (arrProDesc != null && arrProDesc.Length > 0)
            {
                propertyDsc = ((DescriptionAttribute)arrProDesc[0]).Description;
            }
            return propertyDsc;
        }
    }
调用方法:

LogManager<T_UserInfo> log = new LogManager<T_UserInfo>();
log.AddLog(currentUserName, OperateType.Del, appUser, null);

枚举:

public enum OperateType
{
/// <summary>
/// 增加
/// </summary>
[Description("增加")]
Add = 1,

/// <summary>
/// 修改
/// </summary>
[Description("修改")]
Edit = 2,

/// <summary>
/// 删除
/// </summary>
[Description("删除")]
Del = 3,

/// <summary>
/// 查询
/// </summary>
[Description("查询")]
Query = 4
}

 建表:

public class T_OperateLog
{
public T_OperateLog()
{
CreateDate = DateTime.Now;
}

public int LogId { get; set; }

/// <summary>
/// 操作者
/// </summary>
[MaxLength(50)]
public string OperateUser { get; set; }

/// <summary>
/// 操作类型
/// </summary>
public OperateType OperateType { get; set; }

public string TblName { get; set; }

public string TblNameDesc { get; set; }

/// <summary>
/// 操作时间
/// </summary>
public DateTime CreateDate { get; set; }

/// <summary>
/// 备注
/// </summary>
[MaxLength(500)]
public string Remark { get; set; }
}

public class LogDetail
{
public LogDetail()
{
CreateDate = DateTime.Now;
}

public int ID { get; set; }

public int LogId { get; set; }

public string ColumnName { get; set; }

public string ColumnDesc { get; set; }

public string OldVal { get; set; }

public string NewVal { get; set; }

public DateTime CreateDate { get; set; }
}

效果:

 

猜你喜欢

转载自www.cnblogs.com/nayilvyangguang/p/11983084.html