MSCRM 4.0 原生無審計日志功能。本文介紹一款成熟的 數據審核插件,通過註冊 Pre/Post Operation 插件攔截 Create/Update/Delete 消息,對比屬性變更,將舊值/新值/操作用戶/時間寫入自定義 SystemLog 實體,並提供配置界面動態控制審核範圍 (實體/字段級)。
LogConfig (日志管理) — 維護審核實體與字段白名單。SystemLog (系統日志) — 記錄 EntityName, FieldName, OldValue, NewValue, Operator, OperationTime, OperationType。PreOperation (捕獲舊值) 與 PostOperation (寫入日志) 階段。| 實體 | 字段 | 舊值 | 新值 | 操作用戶 | 時間 | 操作類型 |
|---|---|---|---|---|---|---|
| account | name | 舊客戶名 | 新客戶名 | 張三 | 2010-01-15 10:30 | Update |
| contact | emailaddress1 | (空) | a@b.com | 李四 | 2010-01-15 10:35 | Create |
| opportunity | statecode | Open | Won | 王五 | 2010-01-15 10:40 | Update |
// 插件核心邏輯 (簡化)
public void Execute(IPluginExecutionContext context)
{
if (context.MessageName == "Update" && context.InputParameters.Contains("Target"))
{
var target = (DynamicEntity)context.InputParameters["Target"];
var preImage = context.PreEntityImages["PreImage"]; // 需註冊預像
var postImage = context.PostEntityImages["PostImage"];
foreach (var attr in target.Properties)
{
if (IsFieldAudited(target.Name, attr.Key)) // 讀取配置緩存
{
var oldVal = preImage.Properties.Contains(attr.Key) ? preImage[attr.Key] : null;
var newVal = postImage.Properties.Contains(attr.Key) ? postImage[attr.Key] : null;
if (!Object.Equals(oldVal, newVal))
{
CreateLogEntry(target.Name, attr.Key, oldVal, newVal, context.InitiatingUserId, "Update");
}
}
}
}
}
Asynchronous,避免阻塞主業務事務。Create。SystemLog 實體僅系統管理員可讀,防止數據洩露。文末提供:插件源碼 (C#)、部署管理器安裝包、配置實體定義 XML、註冊工具配置文件。歡迎下載集成。