public override async Task Invoke(AspectContext context, AspectDelegate next) { _ = context ?? throw new ArgumentNullException(nameof(context)); _ = next ?? throw new ArgumentNullException(nameof(next)); IAuditLogService auditLogService = context.ServiceProvider.GetRequiredService <IAuditLogService>(); ITimeService timeService = context.ServiceProvider.GetRequiredService <ITimeService>(); var record = new AuditLogRecord { ApplicationName = this.ApplicationName ?? context.ServiceProvider.GetRequiredService <IHostEnvironment>().ApplicationName, FunctionName = this.FunctionName ?? context.ImplementationMethod.DeclaringType.FullName, OperationName = this.OperationName ?? context.ServiceMethod.Name, StartTime = await timeService.Current(), Operator = Thread.CurrentPrincipal?.Identity?.Name, RequestIp = "127.0.0.1", Arguments = buildInputArguments(context) }; try { await next.Invoke(context); record.Success = true; record.Message = this.BuildMessage(context); record.EndTime = await timeService.Current(); record.Result = buildResult(context); await auditLogService.LogRecord(record); } catch (Exception e) { record.Message = e.Message; record.Success = false; record.EndTime = await timeService.Current(); record.Result = ExceptionInfo.FromException(e); await auditLogService.LogRecord(record); throw; } }
public static DateTimeOffset UtcNow(this ITimeService timeService) { _ = timeService ?? throw new ArgumentNullException(nameof(timeService)); return(timeService.Current().Result); }