internal void AddLogItem(AppLogItem appLogItem) { if (!_messageQueue.IsAddingCompleted) { _messageQueue.Add(appLogItem, _cancellationTokenSource.Token); } }
private static void setStateJson <TState>(TState state, AppLogItem appLogItem) { try { appLogItem.StateJson = JsonSerializer.Serialize( state, new JsonSerializerOptions { IgnoreNullValues = true, WriteIndented = true }); } catch { // don't throw exceptions from logger } }
private static void setStateJson <TState>(TState state, AppLogItem appLogItem) { try { appLogItem.StateJson = JsonConvert.SerializeObject( state, Formatting.Indented, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Include }); } catch { // don't throw exceptions from logger } }
private void saveLogItem(AppLogItem appLogItem) { try { // We need a separate context for the logger to call its SaveChanges several times, // without using the current request's context and changing its internal state. _serviceProvider.RunScopedService <IUnitOfWork>(context => { context.Set <AppLogItem>().Add(appLogItem); context.SaveChanges(); }); } catch { // don't throw exceptions from logger } }
public void Log <TState>( LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter) { if (!IsEnabled(logLevel)) { return; } if (formatter == null) { throw new ArgumentNullException(nameof(formatter)); } var message = formatter(state, exception); if (exception != null) { message = $"{message}{Environment.NewLine}{exception}"; } if (string.IsNullOrEmpty(message)) { return; } var httpContextAccessor = _serviceProvider.GetService <IHttpContextAccessor>(); var appLogItem = new AppLogItem { Url = httpContextAccessor?.HttpContext != null?httpContextAccessor.HttpContext.Request.Path.ToString() : string.Empty, EventId = eventId.Id, LogLevel = logLevel.ToString(), Logger = _loggerName, Message = message }; var props = httpContextAccessor?.GetShadowProperties(); setStateJson(state, appLogItem); _loggerProvider.AddLogItem(new LoggerItem { Props = props, AppLogItem = appLogItem }); }
private void saveLogItem(AppLogItem appLogItem) { try { // We need a separate context for the logger to call its SaveChanges several times, // without using the current request's context and changing its internal state. using (var serviceScope = _scopeFactory.CreateScope()) { using (var context = serviceScope.ServiceProvider.GetService <ExperienceManagementDbContext>()) { context.Set <AppLogItem>().Add(appLogItem); context.SaveChanges(); } } } catch { // don't throw exceptions from logger } }