| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- using System;
- using System.Diagnostics;
- using System.Threading.Tasks;
- using Abp.Aspects;
- using Abp.Threading;
- using Castle.DynamicProxy;
- namespace Abp.Auditing
- {
- internal class AuditingInterceptor : IInterceptor
- {
- private readonly IAuditingHelper _auditingHelper;
- public AuditingInterceptor(IAuditingHelper auditingHelper)
- {
- _auditingHelper = auditingHelper;
- }
- public void Intercept(IInvocation invocation)
- {
- if (AbpCrossCuttingConcerns.IsApplied(invocation.InvocationTarget, AbpCrossCuttingConcerns.Auditing))
- {
- invocation.Proceed();
- return;
- }
- if (!_auditingHelper.ShouldSaveAudit(invocation.MethodInvocationTarget))
- {
- invocation.Proceed();
- return;
- }
- var auditInfo = _auditingHelper.CreateAuditInfo(invocation.TargetType, invocation.MethodInvocationTarget, invocation.Arguments);
- if (invocation.Method.IsAsync())
- {
- PerformAsyncAuditing(invocation, auditInfo);
- }
- else
- {
- PerformSyncAuditing(invocation, auditInfo);
- }
- }
- private void PerformSyncAuditing(IInvocation invocation, AuditInfo auditInfo)
- {
- var stopwatch = Stopwatch.StartNew();
- try
- {
- invocation.Proceed();
- }
- catch (Exception ex)
- {
- auditInfo.Exception = ex;
- throw;
- }
- finally
- {
- stopwatch.Stop();
- auditInfo.ExecutionDuration = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);
- _auditingHelper.Save(auditInfo);
- }
- }
- private void PerformAsyncAuditing(IInvocation invocation, AuditInfo auditInfo)
- {
- var stopwatch = Stopwatch.StartNew();
- invocation.Proceed();
- if (invocation.Method.ReturnType == typeof(Task))
- {
- invocation.ReturnValue = InternalAsyncHelper.AwaitTaskWithFinally(
- (Task) invocation.ReturnValue,
- exception => SaveAuditInfo(auditInfo, stopwatch, exception)
- );
- }
- else //Task<TResult>
- {
- invocation.ReturnValue = InternalAsyncHelper.CallAwaitTaskWithFinallyAndGetResult(
- invocation.Method.ReturnType.GenericTypeArguments[0],
- invocation.ReturnValue,
- exception => SaveAuditInfo(auditInfo, stopwatch, exception)
- );
- }
- }
- private void SaveAuditInfo(AuditInfo auditInfo, Stopwatch stopwatch, Exception exception)
- {
- stopwatch.Stop();
- auditInfo.Exception = exception;
- auditInfo.ExecutionDuration = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);
- _auditingHelper.Save(auditInfo);
- }
- }
- }
|