当这个方法在 Controller 中执行时:
[Route("deleteIncCloseOut")]
[HttpDelete]
[AuditApi]
public bool deleteIncidentCloseOut(int ID)
{
try
{
using (ESSDataContext ctx = new ESSDataContext())
{
ctx.DeleteIncidentCloseOut(ID);
this.GetCurrentAuditScope().SetCustomField("Dynamic", new { IncidentCloseOutID = ID });
return true;
}
}
catch (Exception ex)
{
log.Error($"{ex.StackTrace}");
return false;
throw ex;
}
}
存储过程 DeleteIncidentCloseOut(ID)
出现异常,因此未设置 AuditEvent 的 CustomField。但是,DataProvider 的 InsertEvent 仍在执行。
我的问题是在我的 InsertEvent 中我必须填充两个表。一个表包含审计事件的简要说明,另一个包含设置为 AuditEvent 对象的 CustomField 的对象的属性和值。在本例中,我创建了一个以 ID 作为属性的动态对象。因此,我在第一个表中得到一个条目,指出 IncidentCloseOut 已被删除(但实际上它没有,因为抛出了异常),但我没有在第二个表中得到应该删除的事件的 ID(因为那里没有设置 CustomField)。所以我得到了虚假的审计信息。 存储过程最好不会抛出异常,但我希望审计是正确的,即使抛出异常也是如此。
我该如何纠正/改善这种情况?
最佳答案
只需在调用引发异常的存储过程之前设置自定义字段:
public bool deleteIncidentCloseOut(int ID)
{
try
{
using (ESSDataContext ctx = new ESSDataContext())
{
this.GetCurrentAuditScope().SetCustomField("Dynamic", new { IncidentCloseOutID = ID });
ctx.DeleteIncidentCloseOut(ID);
return true;
}
}
catch (Exception ex)
{
...
}
}
关于c# - Audit.Net,-即使抛出异常也需要正确的审计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57356685/