public IActionResult SpecialLog() { MyTestModel testData = new MyTestModel(1, "Test"); elasticSearchService.CheckExistsAndInsert <MyTestModel>("test_model", testData); // Use lowerCase Index Name return(Ok(true)); }
public void LogChanges(ChangeTracker changes) { var modifiedEntities = changes.Entries().Where(p => p.State == EntityState.Modified).ToList(); // Updated Records var deletedEntities = changes.Entries().Where(p => p.State == EntityState.Deleted).ToList(); // Deleted Records var userId = httpcontext.HttpContext.GetAuthUser(); // Get Auth User var now = System.DateTime.UtcNow; foreach (var change in modifiedEntities) // Modified Data { bool LogModelState = change.Entity.GetType().CheckAttributeExist <LogModelAttribute>(); // Check LogModel Attribute var entityName = change.Entity.GetType().Name; // Table name var PrimaryKey = change.OriginalValues.Properties.FirstOrDefault(prop => prop.IsPrimaryKey() == true).Name; //Table Primary Key EntityLog log = new EntityLog(userId, entityName, change.OriginalValues[PrimaryKey].ToInt(), now, ChangeState.Updated); foreach (IProperty prop in change.OriginalValues.Properties) // Updated Props { bool LogPropState = prop.PropertyInfo.CheckAttributeExist <LogPropAttribute>(); // Check LogProp Attribute if (!LogModelState && !LogPropState) { continue; } var originalValue = change.OriginalValues[prop.Name].ToString(); var currentValue = change.CurrentValues[prop.Name]?.ToString(); string a = prop.GetDisplayName(); if (originalValue != currentValue) { log.ChangedValues.Add(new ChangedValues(prop.GetDisplayName(), originalValue, currentValue)); } } elasticSearchService.CheckExistsAndInsert <EntityLog>(IndexType.entity_log, log); // Send to Elastic } foreach (var deleted in deletedEntities) // Deleted Data { var entityName = deleted.Entity.GetType().Name; // Table name bool LogModelState = deleted.Entity.GetType().CheckAttributeExist <LogModelAttribute>(); // Check LogModel Attribute if (!LogModelState) { continue; } var PrimaryKey = deleted.OriginalValues.Properties.FirstOrDefault(prop => prop.IsPrimaryKey() == true).Name; //Table Primary Key EntityLog log = new EntityLog(userId, entityName, deleted.OriginalValues[PrimaryKey].ToInt(), now, ChangeState.Deleted); elasticSearchService.CheckExistsAndInsert <EntityLog>(IndexType.entity_log, log); // Send to Elastic } }
public void OnActionExecuted(ActionExecutedContext context) { if (!HasNoLogAttribute(context) && (LogAllEndPoints || HasLogAttribute(context))) { var(controllerName, actionName, httpType) = context.GetControllerAndActionName(); ActivityLog activity = new ActivityLog { ControllerName = controllerName, ActionName = actionName, HttpType = httpType, StatusCode = (context.Result as ObjectResult)?.StatusCode ?? 0, UserId = context.HttpContext.GetAuthUser(), // UserId Params = requestParams.ToString(), IPAddress = this.HostingEnvironment.IsDevelopment() ? "" : context.HttpContext.Connection.RemoteIpAddress.ToString(), // Get User Ip Address DateCreated = DateTime.Now }; elasticSearchService.CheckExistsAndInsert <ActivityLog>(IndexType.activity_log, activity); // Send to Elastic } }
public async Task <IActionResult> InsertLogForModel(string indexName, ErrorLog logdata) { var result = await elasticSearchService.CheckExistsAndInsert <ErrorLog>(indexName, logdata); return(Ok(result)); }