简体   繁体   English

Audit.Net,-即使抛出异常,也需要正确的审核

[英]Audit.Net, - need correct auditing even when exception is thrown

When this method is executed in the 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;
  }
}

There is an exception with the stored procedure DeleteIncidentCloseOut(ID) , and so the CustomField of the AuditEvent is not being set. 存储过程DeleteIncidentCloseOut(ID)存在一个异常,因此未设置AuditEvent的CustomField。 However, the DataProvider's InsertEvent is still being executed. 但是,DataProvider的InsertEvent仍在执行。

My problem is that in my InsertEvent I have to populate two tables. 我的问题是在我的InsertEvent中,我必须填充两个表。 One table has a brief description of the audit event, and the other contains the properties and values of the object that is set as the CustomField of the AuditEvent object. 一个表简要描述了审核事件,另一个表包含了设置为AuditEvent对象的CustomField的对象的属性和值。 In this case, I create a dynamic object with the ID as the property. 在这种情况下,我将创建一个以ID为属性的动态对象。 So, I get an entry in my first table that the IncidentCloseOut has been deleted (but actually it hasn't, since an exception was thrown), but I do not get the ID of the supposedly deleted event in the second table (since there was no CustomField set). 因此,我在第一个表中获得一个条目,该事件IncidentCloseOut已被删除(但实际上并没有被删除,因为引发了异常),但是我没有在第二个表中获得所谓的已删除事件的ID。没有设置CustomField)。 So I am getting false auditing information. 所以我得到虚假的审计信息。 Preferably, the stored procedure wouldn't throw an exception, but I would like the auditing to be correct, even if an exception is thrown. 最好,存储过程不会抛出异常,但是即使抛出异常,我也希望审计是正确的。

How can I rectify/improve this situation? 如何纠正/改善这种情况?

Just set the custom field before calling the stored procedure that throws the exception: 只需调用引发异常的存储过程之前设置自定义字段即可:

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)
  {
    ...
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM