private string ConvertArgumentsToJson(IDictionary <string, object> arguments) { try { if (arguments.IsNullOrEmpty()) { return("{}"); } var dictionary = new Dictionary <string, object>(); foreach (var argument in arguments) { if (argument.Value != null && IgnoredTypesForSerializationOnAuditLogging.Any(t => t.IsInstanceOfType(argument.Value))) { dictionary[argument.Key] = null; } else { dictionary[argument.Key] = argument.Value; } } return(AuditingHelper.Serialize(dictionary)); } catch (Exception ex) { Logger.Warn(ex.ToString(), ex); return("{}"); } }
private string ConvertArgumentsToJson(IDictionary <string, object> arguments) { try { if (arguments.IsNullOrEmpty()) { return("{}"); } var dictionary = new Dictionary <string, object>(); foreach (var argument in arguments) { if (argument.Value != null && _ignoredTypesForSerialization.Any(t => t.IsInstanceOfType(argument.Value))) { dictionary[argument.Key] = null; } else { dictionary[argument.Key] = argument.Value; } } return(AuditingHelper.Serialize(dictionary)); } catch (Exception ex) { Logger.Warn("Could not serialize arguments for method: " + _auditInfo.ServiceName + "." + _auditInfo.MethodName); Logger.Warn(ex.ToString(), ex); return("{}"); } }
private string ConvertArgumentsToJson(ActionExecutingContext filterContext) { try { if (filterContext.ActionParameters.IsNullOrEmpty()) { return("{}"); } var dictionary = new Dictionary <string, object>(); foreach (var argument in filterContext.ActionParameters) { if (argument.Value != null && IgnoredTypesForSerializationOnAuditLogging.Any(t => t.IsInstanceOfType(argument.Value))) { dictionary[argument.Key] = null; } else { dictionary[argument.Key] = argument.Value; } } return(AuditingHelper.Serialize(dictionary)); } catch (Exception ex) { Logger.Warn("Could not serialize arguments for method: " + filterContext.Controller.GetType().FullName + "." + filterContext.ActionDescriptor.ActionName); Logger.Warn(ex.ToString(), ex); return("{}"); } }
public void Ignored_Properties_Should_Not_Be_Serialized() { var json = AuditingHelper.Serialize(new AuditingHelperTestPersonDto() { FullName = "Derrick Dou", Age = 28, School = new AuditingHelperTestSchoolDto() { Name = "大幕小学", Address = "咸宁大幕" } }); json.ShouldBe("{\"fullName\":\"Derrick Dou\",\"school\":{\"name\":\"大幕小学\"}}"); }
public void Ignored_Properties_Should_Not_Be_Serialized() { var json = AuditingHelper.Serialize(new AuditingHelperTestPersonDto { FullName = "John Doe", Age = 18, School = new AuditingHelperTestSchoolDto { Name = "Crosswell Secondary", Address = "Broadway Ave, West Bend" } }); json.ShouldBe("{\"fullName\":\"John Doe\",\"school\":{\"name\":\"Crosswell Secondary\"}}"); }
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { IAuditingConfiguration configuration = _provider.GetRequiredService <IAuditingConfiguration>(); if (!ShouldSaveAudit(context, configuration)) { await next(); return; } using (CrossCuttingConcerns.Applying(context.Controller, CrossCuttingConcerns.Auditing)) { IAuditStore store = _provider.GetService <IAuditStore>(); IFunction function = context.GetExecuteFunction(); //var auditInfo = store.CreateAuditInfo( // context.ActionDescriptor.AsControllerActionDescriptor().ControllerTypeInfo.AsType(), // context.ActionDescriptor.AsControllerActionDescriptor().MethodInfo, // context.ActionArguments //); Type type = context.ActionDescriptor.AsControllerActionDescriptor().ControllerTypeInfo.AsType(); List <Type> ignoredTypes = configuration.IgnoredTypes; AuditOperationEntry operation = new AuditOperationEntry { FunctionName = function.Name, ClientIpAddress = context.HttpContext.GetClientIp(), UserAgent = context.HttpContext.Request.Headers["User-Agent"].FirstOrDefault(), CreatedTime = DateTime.Now, ServiceName = type != null ? type.FullName : "", Parameters = ConvertArgumentsToJson(context.ActionArguments, ignoredTypes), }; if (context.HttpContext.User.Identity.IsAuthenticated && context.HttpContext.User.Identity is ClaimsIdentity identity) { operation.UserId = identity.GetUserId(); operation.UserName = identity.GetUserName(); } var stopwatch = Stopwatch.StartNew(); ActionExecutedContext result = null; try { result = await next(); if (result.Exception != null && !result.ExceptionHandled) { operation.Exception = result.Exception; } } catch (Exception ex) { operation.Exception = ex; throw; } finally { stopwatch.Stop(); operation.Elapsed = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds); if (configuration.SaveReturnValues && result != null) { switch (result.Result) { case ObjectResult objectResult: operation.ReturnValue = AuditingHelper.Serialize(objectResult.Value, ignoredTypes); break; case JsonResult jsonResult: operation.ReturnValue = AuditingHelper.Serialize(jsonResult.Value, ignoredTypes); break; case ContentResult contentResult: operation.ReturnValue = contentResult.Content; break; case AjaxResult ajaxResult: operation.ReturnValue = ajaxResult.Content; break; } } await store.SaveAsync(operation); } } }