public MainViewModel(Dispatcher dispatcher) { StopThreads = false; Dispatcher = dispatcher; InitializeLogging(true); Title = "OpenLogger Sample MVVM"; LoggerFacade.LogVerbose("Set Title: " + Title); StartLoggingCommand = new RelayCommand(StartLogging, CanStartLogging); StopLoggingCommand = new RelayCommand(StopLogging, CanStopLogging); ExitAppCommand = new RelayCommand(ExitApp); TabItems = new ObservableCollection <CustomTabItem> { new CustomTabItem { Title = "All Logs", Content = GetMasterLoggingView() }, new CustomTabItem { Title = "Tab 1", Content = GetLoggingView() }, new CustomTabItem { Title = "Tab 2", Content = GetLoggingView() } }; }
static void Main(string[] args) { var loggerInstance = Logger.Instance; loggerInstance.Severity = LogSeverity.Debug; loggerInstance.Attach(new LogToDebugger()); loggerInstance.Attach(new LogToConsole()); var logger = new LoggerFacade(loggerInstance, "Sample Console"); var threads = new List <Thread>(5); for (var i = 0; i < threads.Capacity; i++) { threads.Add(new Thread(LogThread)); } foreach (var thread in threads) { try { thread.Start(); logger.LogVerbose("Started Thread Id: " + thread.ManagedThreadId); while (!thread.IsAlive) { ; } Thread.Sleep(750); } catch (Exception ex) { logger.LogFatal("Unable to start thread with index " + threads.IndexOf(thread), ex); } } Thread.Sleep(2000); foreach (var thread in threads.Where(x => x.IsAlive)) { logger.LogVerbose("Aborting Thread Id: " + thread.ManagedThreadId); thread.Abort(); logger.LogVerbose("Waiting for Thread Id: " + thread.ManagedThreadId); thread.Join(); } Console.ReadKey(); }
static void LogThread() { var logger = new LoggerFacade(Logger.Instance, "Thread Id: " + Thread.CurrentThread.ManagedThreadId); while (true) { logger.LogVerbose("Hello World!"); Thread.Sleep(500); } }
public LoggingViewModel(Dispatcher dispatcher, LoggerFacade loggerFacade) { this.Dispatcher = dispatcher; Title = "Logging"; LoggerFacade = loggerFacade; // Option 1: Pass facade in since Group is defined outside LoggerViewModel LoggerFacade.Logger = Logger.Instance; logGroupId = loggerFacade.GroupId; Logger.Instance.Attach(this); logSeverities = new ObservableCollection <LogSeverity>(Enum.GetValues(typeof(LogSeverity)).Cast <LogSeverity>()); logOrder = LogOrderBy.Ascending; defaultSelectedLogSeverities = new ObservableCollection <LogSeverity>(Enum.GetValues(typeof(LogSeverity)).Cast <LogSeverity>());; RefreshLogCollection(); LoggerFacade.LogVerbose("Initialized Group Logging"); LogFilterChangedCommand = new RelayCommand(LogFilterChanged); CopyLogItemCommand = new RelayCommand <LogEventArgs>(CopyLogItem); CopyAllLogItemsCommand = new RelayCommand(CopyAllLogItems); }
void CreateSomeLogs(int groupId) { var rand = new Random(groupId); LogSeverity severity; var logger = new LoggerFacade(Logger.Instance, "Thread [" + Thread.CurrentThread.ManagedThreadId + "]") { GroupId = groupId }; while (!StopThreads) { try { severity = (LogSeverity)rand.Next(1, 7); // 7 will generate an exception switch (severity) { case LogSeverity.Debug: logger.LogDebug("Debug Message from Group Id: " + groupId); break; case LogSeverity.Fatal: logger.LogFatal("Fatal Message from Group Id: " + groupId); break; case LogSeverity.Error: logger.LogError("Error Message from Group Id: " + groupId); break; case LogSeverity.Warning: logger.LogWarning("Warning Message from Group Id: " + groupId); break; case LogSeverity.Verbose: logger.LogVerbose("Verbose Message from Group Id: " + groupId); break; case LogSeverity.Info: logger.LogInfo("Info Message from Group Id: " + groupId); break; default: logger.LogDebug("Default Message from Group Id: " + groupId); break; } } catch (ThreadAbortException ex) { logger.LogError("Thread was aborted.", ex); break; } catch (Exception ex) { logger.LogError("Exception encountered while creating a threaded log entry.", ex); break; } Thread.Sleep(rand.Next(1000, 3000)); } logger.LogInfo("Thread Cancelled"); }