Exemplo n.º 1
0
        static void Main(string[] args)
        {
            int totalThreads        = 50;
            TaskCreationOptions tco = TaskCreationOptions.None;
            Task task = null;

            logger.Info("Enter Main");

            Task[] allTasks = new Task[totalThreads];
            for (int i = 0; i < totalThreads; i++)
            {
                int ii = i;
                task = Task.Factory.StartNew(() =>
                {
                    Logger innerLogger = LogManager.GetLogger(ii.ToString());
                    MDC.Set("id", "_" + ii.ToString() + "_");
                    innerLogger.Info("Enter delegate.  i = {0}", ii);
                    innerLogger.Info("Hello! from delegate.  i = {0}", ii);
                    innerLogger.Info("Exit delegate.  i = {0}", ii);
                    MDC.Remove("id");
                });

                allTasks[i] = task;
            }

            logger.Info("Wait on tasks");

            Task.WaitAll(allTasks);

            logger.Info("Tasks finished");

            logger.Info("Exit Main");
        }
Exemplo n.º 2
0
        public void MDCTest2()
        {
            List <Exception> exceptions = new List <Exception>();
            ManualResetEvent mre        = new ManualResetEvent(false);
            int counter   = 500;
            int remaining = counter;

            for (int i = 0; i < counter; ++i)
            {
                ThreadPool.QueueUserWorkItem(
                    s =>
                {
                    try
                    {
                        MDC.Clear();
                        Assert.IsFalse(MDC.Contains("foo"));
                        Assert.AreEqual(string.Empty, MDC.Get("foo"));
                        Assert.IsFalse(MDC.Contains("foo2"));
                        Assert.AreEqual(string.Empty, MDC.Get("foo2"));

                        MDC.Set("foo", "bar");
                        MDC.Set("foo2", "bar2");

                        Assert.IsTrue(MDC.Contains("foo"));
                        Assert.AreEqual("bar", MDC.Get("foo"));

                        MDC.Remove("foo");
                        Assert.IsFalse(MDC.Contains("foo"));
                        Assert.AreEqual(string.Empty, MDC.Get("foo"));

                        Assert.IsTrue(MDC.Contains("foo2"));
                        Assert.AreEqual("bar2", MDC.Get("foo2"));
                    }
                    catch (Exception ex)
                    {
                        lock (exceptions)
                        {
                            exceptions.Add(ex);
                        }
                    }
                    finally
                    {
                        if (Interlocked.Decrement(ref remaining) == 0)
                        {
                            mre.Set();
                        }
                    }
                });
            }

            mre.WaitOne();
            if (exceptions.Count != 0)
            {
                Assert.Fail(exceptions[0].ToString());
            }
        }
Exemplo n.º 3
0
        private void RemoveMdcProperties()
        {
            if (!string.IsNullOrEmpty(ApplicationId))
            {
                MDC.Remove(APP_ID);
            }

            MDC.Remove(YEAR);
            MDC.Remove(MONTH);
            MDC.Remove(DAY);
            MDC.Remove(HOUR);
        }
Exemplo n.º 4
0
        public void MDCTest2()
        {
            List <Exception> exceptions = new List <Exception>();
            ManualResetEvent mre        = new ManualResetEvent(false);
            int counter   = 100;
            int remaining = counter;

            for (int i = 0; i < counter; ++i)
            {
                ThreadPool.QueueUserWorkItem(
                    s =>
                {
                    try
                    {
                        MDC.Clear();
                        Assert.False(MDC.Contains("foo"));
                        Assert.Equal(string.Empty, MDC.Get("foo"));
                        Assert.False(MDC.Contains("foo2"));
                        Assert.Equal(string.Empty, MDC.Get("foo2"));

                        MDC.Set("foo", "bar");
                        MDC.Set("foo2", "bar2");

                        Assert.True(MDC.Contains("foo"));
                        Assert.Equal("bar", MDC.Get("foo"));

                        MDC.Remove("foo");
                        Assert.False(MDC.Contains("foo"));
                        Assert.Equal(string.Empty, MDC.Get("foo"));

                        Assert.True(MDC.Contains("foo2"));
                        Assert.Equal("bar2", MDC.Get("foo2"));

                        Assert.Null(MDC.GetObject("foo3"));
                    }
                    catch (Exception ex)
                    {
                        lock (exceptions)
                        {
                            exceptions.Add(ex);
                        }
                    }
                    finally
                    {
                        if (Interlocked.Decrement(ref remaining) == 0)
                        {
                            mre.Set();
                        }
                    }
                });
            }

            mre.WaitOne();
            StringBuilder exceptionsMessage = new StringBuilder();

            foreach (var ex in exceptions)
            {
                if (exceptionsMessage.Length > 0)
                {
                    exceptionsMessage.Append("\r\n");
                }

                exceptionsMessage.Append(ex.ToString());
            }

            Assert.True(exceptions.Count == 0, exceptionsMessage.ToString());
        }