コード例 #1
0
        public static string GetContext()
        {
            if (!MappedDiagnosticsContext.Contains(TagologContextParamName))
            {
                return(string.Empty);
            }

            return(MappedDiagnosticsContext.Get(TagologContextParamName));
        }
コード例 #2
0
        public void MDCTest1()
        {
            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
                    {
                        MappedDiagnosticsContext.Clear();
                        Assert.IsFalse(MappedDiagnosticsContext.Contains("foo"));
                        Assert.AreEqual(string.Empty, MappedDiagnosticsContext.Get("foo"));
                        Assert.IsFalse(MappedDiagnosticsContext.Contains("foo2"));
                        Assert.AreEqual(string.Empty, MappedDiagnosticsContext.Get("foo2"));

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

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

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

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

            mre.WaitOne();
            Assert.AreEqual(0, exceptions.Count);
        }
コード例 #3
0
        public void dispose_is_idempotent()
        {
            const string itemKey = "itemKey";

            MappedDiagnosticsContext.Clear();
            IDisposable disposable = MappedDiagnosticsContext.SetScoped(itemKey, "item1");

            disposable.Dispose();
            Assert.False(MappedDiagnosticsContext.Contains(itemKey));

            //This item shouldn't be removed since it is not the disposable one
            MappedDiagnosticsContext.Set(itemKey, "item2");
            disposable.Dispose();

            Assert.True(MappedDiagnosticsContext.Contains(itemKey));
        }
コード例 #4
0
ファイル: CommandExecutor.cs プロジェクト: z00nx/Sonarr
        private void ExecuteCommand <TCommand>(Command command) where TCommand : Command
        {
            var handlerContract = typeof(IExecute <>).MakeGenericType(command.GetType());
            var handler         = (IExecute <TCommand>)_serviceFactory.Build(handlerContract);

            _logger.Trace("{0} -> {1}", command.GetType().Name, handler.GetType().Name);

            try
            {
                _trackCommands.Start(command);
                BroadcastCommandUpdate(command);

                if (!MappedDiagnosticsContext.Contains("CommandId") && command.SendUpdatesToClient)
                {
                    MappedDiagnosticsContext.Set("CommandId", command.Id.ToString());
                }

                handler.Execute((TCommand)command);

                if (command.State == CommandStatus.Running)
                {
                    _trackCommands.Completed(command);
                }
            }
            catch (Exception e)
            {
                _trackCommands.Failed(command, e);
                throw;
            }
            finally
            {
                BroadcastCommandUpdate(command);
                _eventAggregator.PublishEvent(new CommandExecutedEvent(command));

                if (MappedDiagnosticsContext.Get("CommandId") == command.Id.ToString())
                {
                    MappedDiagnosticsContext.Remove("CommandId");
                }
            }

            _logger.Trace("{0} <- {1} [{2}]", command.GetType().Name, handler.GetType().Name, command.Runtime.ToString(""));
        }
コード例 #5
0
        public void MDCTest1()
        {
            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
                    {
                        MappedDiagnosticsContext.Clear();
                        Assert.False(MappedDiagnosticsContext.Contains("foo"));
                        Assert.Equal(string.Empty, MappedDiagnosticsContext.Get("foo"));
                        Assert.False(MappedDiagnosticsContext.Contains("foo2"));
                        Assert.Equal(string.Empty, MappedDiagnosticsContext.Get("foo2"));
                        Assert.Equal(0, MappedDiagnosticsContext.GetNames().Count);

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

                        Assert.True(MappedDiagnosticsContext.Contains("foo"));
                        Assert.Equal("bar", MappedDiagnosticsContext.Get("foo"));
                        Assert.Equal(2, MappedDiagnosticsContext.GetNames().Count);

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

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

                        Assert.Equal(1, MappedDiagnosticsContext.GetNames().Count);
                        Assert.True(MappedDiagnosticsContext.GetNames().Contains("foo2"));

                        Assert.Null(MappedDiagnosticsContext.GetObject("foo3"));
                        MappedDiagnosticsContext.Set("foo3", new { One = 1 });
                    }
                    catch (Exception exception)
                    {
                        lock (exceptions)
                        {
                            exceptions.Add(exception);
                        }
                    }
                    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());
        }