public void ContextLoggerOutputsHighestLogLevelCorrectly(LogLevel[] levels, LogLevel expected) { const string message = "This is a test"; using var context = new ActionContext("default", "TestContext"); var logger = new ContextLogger(context); foreach (var level in levels) { logger.Log(level, message); } Assert.NotNull(logger); Assert.Equal(levels.Length, logger.LogEntries.Count); Assert.Equal(expected, logger.GetHighestLogLevel()); }
public void ContextLoggerLogOutputsLogLevel(LogLevel level) { const string message = "This is a test"; using var context = new ActionContext("default", "TestContext"); Thread.Sleep(10); var logger = new ContextLogger(context); logger.Log(level, message); var entry = logger.LogEntries?.FirstOrDefault(); Assert.NotNull(entry); Assert.Single(logger.LogEntries); Assert.Equal(level, entry.LogLevel); Assert.Equal(message, entry.Message); Assert.Equal(context.Info.ContextName, entry.ContextName); Assert.Equal(context.Info.Id, entry.ContextId); Assert.True(entry.TimeElapsed.TotalMilliseconds > 10); }
public void ContextLoggerOutputsSummaryCorrectly(LogLevel[] levels, LogLevel expectedLevel, string expectedMessage) { const string message = "This is a test"; using var context = new ActionContext("default", "TestContext"); Thread.Sleep(10); var logger = new ContextLogger(context); foreach (var level in levels) { logger.Log(level, message); } var summary = logger.GetSummaryLogEntry(); Assert.NotNull(logger); Assert.Equal(levels.Length, logger.LogEntries.Count); Assert.Equal(context.Info.ContextName, summary.ContextName); Assert.Equal(context.Info.Id, summary.ContextId); Assert.Equal(expectedLevel, summary.LogLevel); Assert.StartsWith($"The context '{context.Info.ContextName}' ended {expectedMessage}", summary.Message); Assert.True(summary.TimeElapsed.TotalMilliseconds > 10); }
private static void RunOptions(CommandLineOptions options) { // Build Container IContainer container = PluginManager.Compose(); // Initialize Context Logger LogHelper logHelper = container.Resolve <LogHelper>(); ContextLogger logger = logHelper.CreateContextLogger(Application.Current); // Test Logging string appVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString(); logger.Log(LogEventLevel.Information, "Ruminoid Studio - 版本{AppVersion}", appVersion); // Add Event Handler Application.Current.DispatcherUnhandledException += (sender, args) => { args.Handled = true; logger.LogException(LogEventLevel.Error, "发生了一个故障。", args.Exception); new TaskDialog { EnableHyperlinks = false, MainInstruction = "发生了一个故障。", WindowTitle = "灾难性故障", Content = args.Exception.Message, MainIcon = TaskDialogIcon.Error, MinimizeBox = false, Buttons = { new TaskDialogButton(ButtonType.Ok) } }.ShowDialog(); }; AppDomain.CurrentDomain.UnhandledException += (sender, args) => { if (!(args.ExceptionObject is Exception)) { return; } logger.LogException(LogEventLevel.Fatal, "发生了一个故障。", (Exception)args.ExceptionObject); new TaskDialog { EnableHyperlinks = false, MainInstruction = "发生了一个故障。", WindowTitle = "灾难性故障", Content = ((Exception)args.ExceptionObject).Message ?? "Exception", MainIcon = TaskDialogIcon.Error, MinimizeBox = false, Buttons = { new TaskDialogButton(ButtonType.Ok) } }.ShowDialog(); }; // Load Project logger.Log(LogEventLevel.Warning, "Lifecycle Helper: 开始加载项目。"); ProjectManager projectManager = container.Resolve <ProjectManager>(); projectManager.Load(options.ProjectFile); // Change Default Culture Resources.Culture = CultureInfo.CurrentUICulture; // Register Theme Service ThemeService.Current.Register(Application.Current, Theme.Windows, Accent.Windows); // Initialize Main Window Application.Current.MainWindow = new MainWindow(); Application.Current.MainWindow.Show(); // Change Default Theme Application.Current.Dispatcher?.Invoke(() => ThemeService.Current.ChangeTheme(Theme.Dark)); Application.Current.Dispatcher?.Invoke(() => ThemeService.Current.ChangeAccent(Accent.Blue)); }