EventsManager
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using DevExpress.Web.ASPxGridView;
using COM.SYNACAST.PPBOSS.SSOLib;
/// <summary>
/// EventsManager 的摘要说明
/// </summary>
public sealed class EventsManager {
/// <summary>
/// 日志自助服务最多记录相关列数.
/// </summary>
private static readonly int AffectedColumns = 5;
private static readonly int ThumbnailLength = int.MaxValue;
private static readonly int ThumbnailTextLength;
static EventsManager() {
ThumbnailTextLength = ThumbnailLength - 3;
}
private static readonly string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["BOSSConnection"].ConnectionString;
private EventsManager() {
}
#region 日志手动服务
public static void LogEvent(HttpContext context, string record, int type) {
try {
if (context.Session["UserID"] != null) {
using (SqlConnection con = new SqlConnection(connectionString)) {
using (SqlCommand cmd = new SqlCommand("BOSS_Event_LogEvent", con)) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("UserName", context.Session["UserName"]));
cmd.Parameters.Add(new SqlParameter("UserId", (int)context.Session["UserID"]));
cmd.Parameters.Add(new SqlParameter("ResourceId", ResourcesManager.GetResourceId(context)));
cmd.Parameters.Add(new SqlParameter("Url", context.Request.Url.AbsoluteUri));
cmd.Parameters.Add(new SqlParameter("IP", Common.GetIP()));
cmd.Parameters.Add(new SqlParameter("Record", record));
cmd.Parameters.Add(new SqlParameter("Type", type));
con.Open();
cmd.ExecuteNonQuery();
}
}
}
}
catch { }
}
public static void LogEvent(HttpContext context, string record, int type, string appName, string menuName) {
try {
if (context.Session["UserID"] != null) {
using (SqlConnection con = new SqlConnection(connectionString)) {
using (SqlCommand cmd = new SqlCommand(@"INSERT INTO BOSS_Event (E_U_LoginName, E_UserID, E_DateTime, E_ApplicationID, E_A_AppName, E_M_Name, E_IP, E_Record, E_From, E_Type)
VALUES (@UserName,@UserId,GETDATE(),@AppId,@AppName,@MenuName,@IP,@Record,@Url,@Type)", con)) {
cmd.Parameters.Add(new SqlParameter("UserName", context.Session["UserName"]));
cmd.Parameters.Add(new SqlParameter("UserId", (int)context.Session["UserID"]));
cmd.Parameters.Add(new SqlParameter("AppId", DBNull.Value));
cmd.Parameters.Add(new SqlParameter("AppName", appName));
cmd.Parameters.Add(new SqlParameter("MenuName", menuName));
cmd.Parameters.Add(new SqlParameter("IP", Common.GetIP()));
cmd.Parameters.Add(new SqlParameter("Record", record));
cmd.Parameters.Add(new SqlParameter("Url", context.Request.Url.AbsoluteUri));
cmd.Parameters.Add(new SqlParameter("Type", type));
con.Open();
cmd.ExecuteNonQuery();
}
}
}
}
catch { }
}
#endregion
#region 日志自助服务
/// <summary>
/// 注册到日志管理者.
/// 注册控件的更新、插入、删除完成事件将记入日志.
/// </summary>
/// <param name="agvs">注册的控件. 建议用 ASPxGridView.SettingsText.Title 属性描述它所绑定的表对象.</param>
public static void RegistASPxGridViews(params ASPxGridView[] agvs) {
foreach (ASPxGridView agv in agvs) {
RegistASPxGridView(agv);
}
}
/// <summary>
/// 注册到日志管理者.
/// 注册控件的更新、插入、删除完成事件将记入日志.
/// </summary>
/// <param name="agv">注册的控件. 建议用 ASPxGridView.SettingsText.Title 属性描述它所绑定的表对象.</param>
public static void RegistASPxGridView(ASPxGridView agv) {
agv.RowUpdated += new DevExpress.Web.Data.ASPxDataUpdatedEventHandler(agv_RowUpdated);
agv.RowInserted += new DevExpress.Web.Data.ASPxDataInsertedEventHandler(agv_RowInserted);
agv.RowDeleted += new DevExpress.Web.Data.ASPxDataDeletedEventHandler(agv_RowDeleted);
}
private static void agv_RowDeleted(object sender, DevExpress.Web.Data.ASPxDataDeletedEventArgs e) {
ASPxGridView agv = sender as ASPxGridView;
System.Text.StringBuilder sb = new System.Text.StringBuilder(string.Format("删除[{0}]", GetTitle(agv.ID, agv.SettingsText.Title)));
string[] keys = new string[e.Values.Count];
object[] values = new object[keys.Length];
e.Values.Keys.CopyTo(keys, 0);
e.Values.Values.CopyTo(values, 0);
for (int i = 0; i < keys.Length && i < AffectedColumns; i++) {
sb.Append(string.Format(", [{0}]{1}", GetTitle(keys[i], agv.Columns[keys[i]].Caption), GetThumbnail(values[i])));
}
LogEvent(agv.Page, sb.ToString());
}
private static void agv_RowInserted(object sender, DevExpress.Web.Data.ASPxDataInsertedEventArgs e) {
ASPxGridView agv = sender as ASPxGridView;
System.Text.StringBuilder sb = new System.Text.StringBuilder(string.Format("插入[{0}]", GetTitle(agv.ID, agv.SettingsText.Title)));
string[] keys = new string[e.NewValues.Count];
object[] values = new object[keys.Length];
e.NewValues.Keys.CopyTo(keys, 0);
e.NewValues.Values.CopyTo(values, 0);
for (int i = 0; i < keys.Length && i < AffectedColumns; i++) {
sb.Append(string.Format(", [{0}]{1}", GetTitle(keys[i], agv.Columns[keys[i]].Caption), GetThumbnail(values[i])));
}
LogEvent(agv.Page, sb.ToString());
}
private static void agv_RowUpdated(object sender, DevExpress.Web.Data.ASPxDataUpdatedEventArgs e) {
ASPxGridView agv = sender as ASPxGridView;
System.Text.StringBuilder sb = new System.Text.StringBuilder(string.Format("更新[{0}]", GetTitle(agv.ID, agv.SettingsText.Title)));
string[] keys = new string[e.NewValues.Count];
object[] values = new object[keys.Length];
e.NewValues.Keys.CopyTo(keys, 0);
e.NewValues.Values.CopyTo(values, 0);
for (int i = 0; i < keys.Length && i < AffectedColumns; i++) {
sb.Append(string.Format(", [{0}]{1}", GetTitle(keys[i], agv.Columns[keys[i]].Caption), GetThumbnail(values[i])));
}
LogEvent(agv.Page, sb.ToString());
}
private static string GetThumbnail(string source) {
return source.Length > ThumbnailLength ? string.Format("{0}", source.Substring(0, ThumbnailTextLength)) : source;
}
private static string GetThumbnail(object source) {
return source == null? string.Empty : GetThumbnail(source.ToString());
}
private static string GetTitle(string oriName, string caption) {
return string.IsNullOrEmpty(caption) ? oriName : caption;
}
private static void LogEvent(Page page, string record) {
try {
using (SqlConnection con = new SqlConnection(connectionString)) {
using (SqlCommand cmd = new SqlCommand("BOSS_Event_LogEvent", con)) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("UserName", page.Session["UserName"]));
cmd.Parameters.Add(new SqlParameter("UserId", (int)page.Session["UserID"]));
cmd.Parameters.Add(new SqlParameter("ResourceId", ResourcesManager.GetResourceId(page)));
cmd.Parameters.Add(new SqlParameter("Url", page.Request.Url.AbsoluteUri));
cmd.Parameters.Add(new SqlParameter("IP", page.Request.UserHostAddress));
cmd.Parameters.Add(new SqlParameter("Record", record));
cmd.Parameters.Add(new SqlParameter("Type", 1));
con.Open();
cmd.ExecuteNonQuery();
}
}
}
catch { }
}
#endregion
}
的运用,使得你完全可以通过在运用时使用一行代码搞定该单据上的所有行为踪迹记录(插入、删除、修改等)。实现该方法的同时注意历史记录的“可读性”,即要能够让人看懂、易懂;因此,其中表名称、列名称使用了 GridView 的 Title、ColumnCaption 等,而不是实际的表、列名称。