Dynamics 365 CE自帶的Audit功能,雖然不會給咱們的業務流程帶來顯著變化,可是這個功能對於咱們追溯數據變化的歷史,診判定製觸發的執行,以及數據還原等,都是不可或缺的關鍵先生。尤爲是涉及到多部門合做,在數據變化方面產生「矛盾」的時候,這個功能將成爲關鍵的證據。可是萬事有利必有弊,這些Audit log,在SQL裏對應的是表記錄,也就是說,須要消耗的你的Storage資源。在Dynamics 365 CE從V8升級到V9以後,你也許會發現升級前夠用的Storage,在升級以後不夠用了,這個的緣由確定方方面面都有,其中一個可能的狀況就是Audit log佔用的Storage變多了。若是在對環境分析以後,發現真的是因爲Audit log的事,那麼就繼續咱們此次的主題,Delete Audit。spa
CE自帶的有Audit Log Management管理功能,裏面會顯示每一個季度的Audit Log Size,而且還提供Delete功能。code
若是你發現Audit Size太大,能夠考慮先把時間比較久的Audit刪除掉,以釋放一部分Storage。那麼若是Audit Size是最近一個季度的比較大呢?因爲Audit Log Management的刪除功能只是以時間段爲條件,不能再細化到Entity級別,這就致使咱們須要考慮如何刪除某個Entity的Audit,以及如何刪除某個Entity,某個時間段以前的Audit。對象
Dynamics 365目前關於Delete Audit提供了兩個API:DeleteRecordChangeHistoryRequest和DeleteAuditDataRequest。blog
DeleteAuditDataRequest是V9之前就有的API,具體的功能有點相似上面的Audit Log Management。就是給一個EndDate,而後發送請求刪除這個時間點以前全部的Audit。下面給個官方Code幫助理解:資源
// Get the list of audit partitions. var partitionRequest =(RetrieveAuditPartitionListResponse)svc.Execute(new RetrieveAuditPartitionListRequest()); AuditPartitionDetailCollection partitions = partitionRequest.AuditPartitionDetailCollection; // Create a delete request with an end date earlier than possible. var deleteRequest = new DeleteAuditDataRequest(); deleteRequest.EndDate = new DateTime(2000, 1, 1); // Check if partitions are not supported as is the case with SQL Server Standard edition. if (partitions.IsLogicalCollection) { // Delete all audit records created up until now. deleteRequest.EndDate = DateTime.Now; } // Otherwise, delete all partitions that are older than the current partition. // Hint: The partitions in the collection are returned in sorted order where the // partition with the oldest end date is at index 0. else { for (int n = partitions.Count - 1; n >= 0; --n) { if (partitions[n].EndDate<DateTime.Now && partitions[n].EndDate>deleteRequest.EndDate) { deleteRequest.EndDate=(DateTime)partitions[n].EndDate; break; } } } // Delete the audit records. if (deleteRequest.EndDate != new DateTime(2000, 1, 1)) { svc.Execute(deleteRequest); Console.WriteLine("Audit records have been deleted."); } else Console.WriteLine("There were no audit records that could be deleted.");
這個API在某些時候可能有用,好比想指定具體的時間點,而不是一個季度一個季度地去刪除。get
DeleteRecordChangeHistoryRequest是V9之後纔有的API,也就是說在使用這個API以前,你得先把你的XRM Dll升級到對應的版本,以及把.Net Framework版本更新到4.6.2,要不你會發現這個對象在使用的時候不存在。這個API的做用,是刪除某條具體Entity Record的全部Audit記錄。用法是指定Target屬性便可,這個是EntityReference對象。it
因爲Dynamics 365不支持直接刪除Audit記錄的操做,就是你若是想先獲取Audit記錄,而後調用Delete方法去刪除某條具體的Audit Record,這個是行不通的。因此綜上來看,咱們只有Audit Log Management以及兩個API可用。那麼再回到上面的問題:如何刪除某個Entity的Audit,以及如何刪除某個Entity,某個時間段以前的Audit?目前來看,只好利用DeleteRecordChangeHistoryRequest來迂迴實現了。io