public virtual IActionResult AddLogMessage( [FromQuery] string loginToken, [FromBody] LogMessage body) { if (string.IsNullOrEmpty(loginToken)) { return(BadRequest("Login token is required")); } var loggedInUser = _tokenizer.ValidateToken(loginToken); if (loggedInUser != null) { if (body != null) { _dbContext.LogMessages.Add(body); _dbContext.SaveChanges(); } if (body != null && body.Id != null) { return(new OkObjectResult(body)); } else { return(NotFound("Record was not added")); } } else { return(BadRequest("Invalid or expired login token")); } }
public void TrimLogs() { using (var context = new LogDataContext()) { var records = context.Logs.AsEnumerable().OrderByDescending(item => DateTime.Parse(item.logged)) .Skip(7500); foreach (var log in records) { context.Remove(log); } context.SaveChanges(); } }
public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter) { if (!IsEnabled(logLevel)) { return; } var optionsBuilder = new DbContextOptionsBuilder <LogDataContext>(); optionsBuilder.UseSqlServer(_connectionsString); using (var context = new LogDataContext(optionsBuilder.Options)) { context.LogMessages.Add(new LogMessage { LogLevel = (int)logLevel, Timestamp = DateTime.UtcNow, Stacktrace = exception?.StackTrace, Source = LogSource.Service.ToString(), Message = formatter(state, exception) }); context.SaveChanges(); } }
/// <summary> /// Adds the component configuration. /// </summary> /// <param name="applicationCode">The application code.</param> /// <param name="component">The component.</param> /// <returns></returns> public bool AddComponentConfiguration(string applicationCode, string component) { try { Component dbCompon = _context.Components.Where(x => x.KeyName.ToUpper() == $"{component}Key".ToUpper()).FirstOrDefault(); Guid componentId = new Guid(); if (dbCompon != null) { componentId = dbCompon.ComponentId; } else { componentId = Guid.NewGuid(); Component comp = new Component() { ComponentId = componentId, DisplayName = component, KeyName = $"{component}Key", Name = component, CreatedDate = DateTime.UtcNow, ModifiedDate = DateTime.UtcNow }; _context.Components.Add(comp); _context.SaveChanges(); } List <ComponentConfiguration> dbComponConfig = _context.ComponentConfigurations.Where(x => x.KeyName.ToUpper() == $"{component}Key".ToUpper()).ToList(); if (dbComponConfig != null) { List <ComponentConfiguration> componConfigList = new List <ComponentConfiguration>(); if (!dbComponConfig.Any(x => Convert.ToString(x.ApplicationCode) == string.Empty && x.KeyName.ToUpper() == $"{component}Key".ToUpper())) { componConfigList.Add(new ComponentConfiguration() { ApplicationCode = string.Empty, ComponentConfigId = Guid.NewGuid(), ComponentId = componentId, KeyName = $"{component}Key", Debug = true, Error = true, Fatal = true, Info = true, IsOmsLog = false, Warn = true, ModifiedDate = DateTime.UtcNow, CreatedDate = DateTime.UtcNow, CreatedBy = Guid.Parse("8A21C95A-553D-49D7-AB7E-1F72F0576B02"), ModifiedBy = Guid.Parse("8A21C95A-553D-49D7-AB7E-1F72F0576B02"), SubscribeDebug = true, SubscribeError = true, CronJobDebug = true, CronJobError = true, CronJobFatal = true, CronJobInfo = true, CronJobWarn = true, }); } if (!string.IsNullOrWhiteSpace(applicationCode)) { if (!dbComponConfig.Any(x => x.ApplicationCode != null && x.ApplicationCode != string.Empty && Convert.ToString(x.ApplicationCode).ToUpper() == applicationCode.ToUpper() && x.KeyName.ToUpper() == $"{component}Key".ToUpper())) { componConfigList.Add(new ComponentConfiguration() { ApplicationCode = applicationCode, ComponentConfigId = Guid.NewGuid(), ComponentId = componentId, KeyName = $"{component}Key", Debug = true, Error = true, Fatal = true, Info = true, IsOmsLog = false, Warn = true, ModifiedDate = DateTime.UtcNow, CreatedDate = DateTime.UtcNow, CreatedBy = Guid.Parse("8A21C95A-553D-49D7-AB7E-1F72F0576B02"), ModifiedBy = Guid.Parse("8A21C95A-553D-49D7-AB7E-1F72F0576B02"), SubscribeDebug = true, SubscribeError = true, CronJobDebug = true, CronJobError = true, CronJobFatal = true, CronJobInfo = true, CronJobWarn = true, }); } } if (componConfigList.Count > 0) { _context.ComponentConfigurations.AddRange(componConfigList); _context.SaveChanges(); } } return(true); } catch (Exception ex) { return(false); throw ex; } }
public void Add(LogEntity log) { _context.Add(log); _context.SaveChanges(); }
public void AddLogMessage(LogMessage logMessage) { _context.LogMessages.Add(logMessage); _context.SaveChanges(); }