public static async Task <object> WrapMonitorMessagingForGenericObject(MonitorLogger _monitorLogger, HttpContext httpContext, object obj, string address, Func <Task <object> > func) { object result = null; var monitorMessage = await MonitorMessageHelper.CreateForGenericObject(httpContext, obj, address); try { try { result = await func(); await MonitorMessageHelper.UpdateForGenericObject(monitorMessage); } catch (MonitorShouldSkipException ex) { MonitorMessageHelper.UpdateShouldSkipMessage(monitorMessage); } } catch (Exception ex) { await MonitorMessageHelper.UpdateForGenericObject(monitorMessage, ex); throw; } finally { await _monitorLogger.WriteMessage(monitorMessage); } return(result); }
/// <summary> /// Adds a monitor logging service /// </summary> /// <param name="serviceCollection"></param> /// <param name="configuration"></param> /// <param name="useAsyncService"></param> /// <returns></returns> public static IServiceCollection AddMonitorLogging(this IServiceCollection serviceCollection, IConfiguration configuration, bool useAsyncService) { var monitorLoggerConfig = configuration.GetTypedSection <MonitorLoggerConfiguration>(); var providers = monitorLoggerConfig.Providers; const string allProviders = "all"; if (String.IsNullOrWhiteSpace(providers)) { providers = allProviders; } providers = providers.ToLower(); if (providers == allProviders || providers.Contains(MonitorLoggerProviderServiceBus.NAME.ToLower())) { serviceCollection.AddSingleton(typeof(IMonitorLoggerProvider), typeof(MonitorLoggerProviderServiceBus)); } if (providers == allProviders || providers.Contains(MonitorLoggerProviderStorageAccount.NAME.ToLower())) { serviceCollection.AddSingleton(typeof(IMonitorLoggerProvider), typeof(MonitorLoggerProviderStorageAccount)); } if (providers == allProviders || providers.Contains(MonitorLoggerProviderAppInsights.NAME.ToLower())) { serviceCollection.AddSingleton(typeof(IMonitorLoggerProvider), typeof(MonitorLoggerProviderAppInsights)); } var sp = serviceCollection.BuildServiceProvider(); var httpContextAccessor = sp.GetService <IHttpContextAccessor>(); var monitorLoggerSender = new MonitorLoggerSender(sp); serviceCollection.AddSingleton <MonitorLoggerSender>(monitorLoggerSender); var monitorLogger = new MonitorLogger(httpContextAccessor, monitorLoggerSender); serviceCollection.TryAddSingleton <MonitorLogger>(monitorLogger); if (!monitorLoggerConfig.IsSynchronous && useAsyncService) { serviceCollection.AddHostedService <MonitorLoggerBackgroundService>(); } return(serviceCollection); }
public ILogger CreateLogger(string categoryName) { var logger = new MonitorLogger(categoryName, _writer); _loggers.Add(logger); return logger; }