Beispiel #1
0
        private async Task AfterInvoke(HttpContext context, bool includeResponseBody, Exception exception)
        {
            var auditAction = context.Items[AuditApiHelper.AuditApiActionKey] as AuditApiAction;
            var auditScope  = context.Items[AuditApiHelper.AuditApiScopeKey] as AuditScope;

            if (auditAction != null && auditScope != null)
            {
                if (exception != null)
                {
                    auditAction.Exception          = exception.GetExceptionInfo();
                    auditAction.ResponseStatusCode = 500;
                    auditAction.ResponseStatus     = "Internal Server Error";
                }
                else if (context.Response != null)
                {
                    var statusCode = context.Response.StatusCode;
                    auditAction.ResponseStatusCode = statusCode;
                    auditAction.ResponseStatus     = AuditApiHelper.GetStatusCodeString(statusCode);
                    if (includeResponseBody && auditAction.ResponseBody == null)
                    {
                        auditAction.ResponseBody = new BodyContent
                        {
                            Type   = context.Response.ContentType,
                            Length = context.Response.ContentLength,
                            Value  = AuditApiHelper.GetResponseBody(context)
                        };
                    }
                }
                // Replace the Action field and save
                (auditScope.Event as AuditEventWebApi).Action = auditAction;
                await auditScope.SaveAsync();
            }
        }
Beispiel #2
0
        private async Task AfterInvoke(HttpContext context, bool includeResponseBody, bool includeResponseHeaders, Exception exception)
        {
#pragma warning disable IDE0019 // Use pattern matching
            var auditAction = context.Items[AuditApiHelper.AuditApiActionKey] as AuditApiAction;
            var auditScope  = context.Items[AuditApiHelper.AuditApiScopeKey] as AuditScope;
#pragma warning restore IDE0019 // Use pattern matching

            if (auditAction != null && auditScope != null)
            {
                if (exception != null)
                {
                    auditAction.Exception          = exception.GetExceptionInfo();
                    auditAction.ResponseStatusCode = 500;
                    auditAction.ResponseStatus     = "Internal Server Error";
                }
                else if (context.Response != null)
                {
                    var statusCode = context.Response.StatusCode;
                    auditAction.ResponseStatusCode = statusCode;
                    auditAction.ResponseStatus     = AuditApiHelper.GetStatusCodeString(statusCode);
                    if (includeResponseBody && auditAction.ResponseBody == null)
                    {
                        auditAction.ResponseBody = new BodyContent
                        {
                            Type   = context.Response.ContentType,
                            Length = context.Response.ContentLength,
                            Value  = await AuditApiHelper.GetResponseBody(context)
                        };
                    }
                }
                if (includeResponseHeaders)
                {
                    auditAction.ResponseHeaders = AuditApiHelper.ToDictionary(context.Response.Headers);
                }
                // Replace the Action field and save
                (auditScope.Event as AuditEventWebApi).Action = auditAction;
                await auditScope.DisposeAsync();
            }
        }