public ILog CreateLog(IDictionary <string, string> properties = null) { var log = _resolver.ResolveAllInstances <ILog>(); var bLogger = new BroadcastLogger(log, _resolver); if (_resolver.IsRegistered <Dictionary <string, string> >("LoggingContext")) { var defaultProperties = _resolver.Resolve <Dictionary <string, string> >("LoggingContext"); bLogger.AddToContext(defaultProperties); } bLogger.AddToContext(properties); return(bLogger); }
public BroadcastLogger(IEnumerable <ILog> logs, IResolve resolver) { _loggers = (from l in logs where l.GetType() != typeof(BroadcastLogger) select l).ToImmutableArray(); _enabled = _loggers.Any(); if (resolver.IsRegistered <IConfiguration>()) { var config = resolver.Resolve <IConfiguration>(); if (config.IsReady) { var levels = config.GetValueOrDefault("Logging:Levels", "NONE"); if (levels.IndexOf("DEBUG", StringComparison.InvariantCultureIgnoreCase) >= 0) { debugEnabled = true; } if (levels.IndexOf("INFO", StringComparison.InvariantCultureIgnoreCase) >= 0) { informationEnabled = true; } if (levels.IndexOf("EVENT", StringComparison.InvariantCultureIgnoreCase) >= 0) { eventEnabled = true; } if (levels.IndexOf("ERROR", StringComparison.InvariantCultureIgnoreCase) >= 0) { errorEnabled = true; } if (levels.IndexOf("FATAL", StringComparison.InvariantCultureIgnoreCase) >= 0) { fatalEnabled = true; } if (levels.IndexOf("ALL", StringComparison.InvariantCultureIgnoreCase) >= 0) { debugEnabled = true; informationEnabled = true; eventEnabled = true; errorEnabled = true; fatalEnabled = true; } } } }
public SerilogLogger(IResolve resolver) { var loggerConfiguration = new LoggerConfiguration(); if (resolver.IsRegistered <IConfiguration>()) { _configuration = resolver.Resolve <IConfiguration>(); if (_configuration.DebugMode) { loggerConfiguration.WriteTo.Debug(); WriteToDebug = true; } //if (resolver.IsRegistered<IAzureServicesConfiguration>()) //{ // var azureConfig = resolver.Resolve<IAzureServicesConfiguration>(); // if (azureConfig != null && azureConfig.ApplicationInsights != null && // azureConfig.ApplicationInsights.Enabled && // !string.IsNullOrEmpty(azureConfig.ApplicationInsights.Key)) // { // loggerConfiguration.WriteTo.ApplicationInsights(azureConfig.ApplicationInsights.Key, // LogEventToTelemetryConverter); // TelemetryDebugWriter.IsTracingDisabled = true; // WriteToAppInsights = true; // } //} if (System.Diagnostics.Debugger.IsAttached) { try { loggerConfiguration.WriteTo.ColoredConsole(); WriteToColoredConsole = true; } catch (Exception) { //optional, swallow if failes } } var levelMap = new Dictionary <string, LogEventLevel>(StringComparer.InvariantCultureIgnoreCase) { { "DEBUG", LogEventLevel.Debug }, { "INFO", LogEventLevel.Information }, { "EVENT", LogEventLevel.Information }, { "ERROR", LogEventLevel.Error }, { "FATAL", LogEventLevel.Fatal }, { "ALL", LogEventLevel.Verbose } }; var levels = _configuration.GetValueOrDefault("Logging:Levels", "NONE"); if (levelMap.TryGetValue(levels, out var logEventLevel)) { loggerConfiguration.MinimumLevel.Is(logEventLevel); } } _logger = loggerConfiguration.CreateLogger(); _contextProperties = new ConcurrentDictionary <string, string>(); }