Пример #1
0
        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();
        }
Пример #2
0
        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");
        }