物件型別轉換成稽核紀錄

物件型別轉換成稽核紀錄

最近常常寫管理系統需要記錄使用者變更資料的內容,透過System.ComponentModel.DataAnnotations就簡單多了。例如有個銀行物件:(ScriptIgnore是用來過濾不需要記錄在LOG中的資訊)

{
    [Required(ErrorMessage = "必填")]
    [DisplayName("SN")]
    [ScriptIgnore]
    public int SN { get; set; }

    [StringLength(5, ErrorMessage = "請勿輸入超過 5 個字")]
    [Required(ErrorMessage = "必填")]
    [DisplayName("銀行代碼")]
    [RegularExpression(@"\d{3,5}", ErrorMessage = "最多5碼數字")]
    public string BankID { get; set; }

    [StringLength(10, ErrorMessage = "請勿輸入超過 10 個字")]
    [Required(ErrorMessage = "必填")]
    [DisplayName("銀行簡稱")]
    public string BankName { get; set; }
}

轉換類別:

{
    public static string Serialize(object obj)
    {
        var sb = new StringBuilder();

        var properties = new AssociatedMetadataTypeTypeDescriptionProvider(obj.GetType()).GetTypeDescriptor(obj.GetType(), obj).GetProperties();
        foreach (PropertyDescriptor p in properties)
        {
            var attri = p.Attributes[typeof(ScriptIgnoreAttribute)];
            var value = p.GetValue(obj);

            string data = "";

            if (attri != null || value == null || String.IsNullOrEmpty(value.ToString()))
            {
                continue;
            }

            if (value is DateTime)
            {
                data = ((DateTime)value).ToString("yyyy/MM/dd");
            }
            else
            {
                data = value.ToString();
            }

            if (value is IList)
            {
                var arr = value as IList;
                if (arr.Count > 0)
                {
                    var sb2 = new StringBuilder();
                    for (int i = 0; i < arr.Count; i++)
                    {
                        sb2.AppendFormat("\t{0}.{1}\r\n", i + 1, serialize2(arr[i]));
                    }
                    sb.AppendFormat("{0}:\r\n{1}", p.DisplayName, sb2.ToString());
                }
            }
            else
            {
                DisplayAttribute display = p.Attributes[typeof(DisplayAttribute)] as DisplayAttribute;
                if (display == null)
                {
                    sb.AppendFormat("[{0}:{1}]\r\n", p.DisplayName, data);
                }
                else
                {
                    sb.AppendFormat("[{0}:{1}]\r\n", display.Name, data);
                }
            }


        }

        return sb.ToString();
    }

    private static string serialize2(object obj)
    {
        var sb = new StringBuilder();

        var properties = new AssociatedMetadataTypeTypeDescriptionProvider(obj.GetType()).GetTypeDescriptor(obj.GetType(), obj).GetProperties();
        foreach (PropertyDescriptor p in properties)
        {
            var attri = p.Attributes[typeof(ScriptIgnoreAttribute)];
            var value = p.GetValue(obj);

            string data = "";

            if (attri != null || value == null || String.IsNullOrEmpty(value.ToString()))
            {
                continue;
            }

            if (value is DateTime)
            {
                data = ((DateTime)value).ToString("yyyy/MM/dd");
            }
            else
            {
                data = value.ToString();
            }

            DisplayAttribute display = p.Attributes[typeof(DisplayAttribute)] as DisplayAttribute;
            if (display == null)
            {
                sb.AppendFormat("[{0}:{1}] ", p.DisplayName, data);
            }
            else
            {
                sb.AppendFormat("[{0}:{1}] ", display.Name, data);
            }

        }

        return sb.ToString();
    }
}

就可以自動組合出:

[銀行代碼:004]
[銀行簡稱:台灣銀行]

這樣子變更資料的時候只要利用原有的物件型別就可以輕鬆記錄LOG了。

 

Dotblogs 的標籤: