internal static (ConsoleLogger Logger, ConsoleSink Sink, ConsoleSink ErrorSink, Func <LogLevel, string> GetLevelPrefix, int WritesPerMsg) SetUp( ConsoleLoggerOptions options = null, SimpleConsoleFormatterOptions simpleOptions = null, ConsoleFormatterOptions systemdOptions = null, JsonConsoleFormatterOptions jsonOptions = null) { // Arrange var sink = new ConsoleSink(); var errorSink = new ConsoleSink(); var console = new TestConsole(sink); var errorConsole = new TestConsole(errorSink); var consoleLoggerProcessor = new TestLoggerProcessor(); consoleLoggerProcessor.Console = console; consoleLoggerProcessor.ErrorConsole = errorConsole; var logger = new ConsoleLogger(_loggerName, consoleLoggerProcessor); logger.ScopeProvider = new LoggerExternalScopeProvider(); logger.Options = options ?? new ConsoleLoggerOptions(); var formatters = new ConcurrentDictionary <string, ConsoleFormatter>(ConsoleLoggerTest.GetFormatters(simpleOptions, systemdOptions, jsonOptions).ToDictionary(f => f.Name)); Func <LogLevel, string> levelAsString; int writesPerMsg; switch (logger.Options.FormatterName) { case ConsoleFormatterNames.Simple: levelAsString = ConsoleLoggerTest.LogLevelAsStringDefault; writesPerMsg = 2; logger.Formatter = formatters[ConsoleFormatterNames.Simple]; break; case ConsoleFormatterNames.Systemd: levelAsString = ConsoleLoggerTest.GetSyslogSeverityString; writesPerMsg = 1; logger.Formatter = formatters[ConsoleFormatterNames.Systemd]; break; case ConsoleFormatterNames.Json: levelAsString = ConsoleLoggerTest.GetJsonLogLevelString; writesPerMsg = 1; logger.Formatter = formatters[ConsoleFormatterNames.Json]; break; default: throw new ArgumentOutOfRangeException(nameof(logger.Options.FormatterName)); } return(logger, sink, errorSink, levelAsString, writesPerMsg); }
public void ConsoleLoggerOptions_TimeStampFormat_IsReloaded() { // Arrange var monitor = new TestOptionsMonitor(new ConsoleLoggerOptions() { FormatterName = "NonExistentFormatter" }); var loggerProvider = new ConsoleLoggerProvider(monitor, ConsoleLoggerTest.GetFormatters()); var logger = (ConsoleLogger)loggerProvider.CreateLogger("Name"); // Act & Assert Assert.Equal("NonExistentFormatter", logger.Options.FormatterName); Assert.Equal(ConsoleFormatterNames.Simple, logger.Formatter.Name); }
internal static (ConsoleLogger Logger, ConsoleSink Sink, ConsoleSink ErrorSink, Func <LogLevel, string> GetLevelPrefix, int WritesPerMsg) SetUp( ConsoleLoggerOptions options = null, SimpleConsoleFormatterOptions simpleOptions = null, ConsoleFormatterOptions systemdOptions = null, JsonConsoleFormatterOptions jsonOptions = null) { // Arrange var sink = new ConsoleSink(); var errorSink = new ConsoleSink(); var console = new TestConsole(sink); var errorConsole = new TestConsole(errorSink); var bufferMode = options == null ? ConsoleLoggerQueueFullMode.Wait : options.QueueFullMode; var maxQueueLength = options == null ? ConsoleLoggerOptions.DefaultMaxQueueLengthValue : options.MaxQueueLength; var consoleLoggerProcessor = new TestLoggerProcessor(console, errorConsole, bufferMode, maxQueueLength); var formatters = new ConcurrentDictionary <string, ConsoleFormatter>(ConsoleLoggerTest.GetFormatters(simpleOptions, systemdOptions, jsonOptions).ToDictionary(f => f.Name)); ConsoleFormatter?formatter = null; var loggerOptions = options ?? new ConsoleLoggerOptions(); Func <LogLevel, string> levelAsString; int writesPerMsg; switch (loggerOptions.FormatterName) { case ConsoleFormatterNames.Simple: levelAsString = ConsoleLoggerTest.LogLevelAsStringDefault; writesPerMsg = 2; formatter = formatters[ConsoleFormatterNames.Simple]; break; case ConsoleFormatterNames.Systemd: levelAsString = ConsoleLoggerTest.GetSyslogSeverityString; writesPerMsg = 1; formatter = formatters[ConsoleFormatterNames.Systemd]; break; case ConsoleFormatterNames.Json: levelAsString = ConsoleLoggerTest.GetJsonLogLevelString; writesPerMsg = 1; formatter = formatters[ConsoleFormatterNames.Json]; break; default: throw new ArgumentOutOfRangeException(nameof(loggerOptions.FormatterName)); } var logger = new ConsoleLogger(_loggerName, consoleLoggerProcessor, formatter, new LoggerExternalScopeProvider(), loggerOptions); return(logger, sink, errorSink, levelAsString, writesPerMsg); }