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

            return(MappedDiagnosticsContext.Get(TagologContextParamName));
        }
コード例 #2
0
        private Command GetCurrentCommand()
        {
            var commandId = MappedDiagnosticsContext.Get("CommandId");

            if (string.IsNullOrWhiteSpace(commandId))
            {
                return(null);
            }

            return(_trackCommands.GetById(commandId));
        }
コード例 #3
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);
        }
コード例 #4
0
ファイル: KafkaTarget.cs プロジェクト: yoyofx/NLog.Kafka
        private static void TransferContextDataToLogEventProperties(Dictionary <string, object> logDic)
        {
            foreach (var contextItemName in MappedDiagnosticsContext.GetNames())
            {
                var key = contextItemName;

                if (!logDic.ContainsKey(key))
                {
                    var value = MappedDiagnosticsContext.Get(key);
                    logDic.Add(key, value);
                }
            }
        }
コード例 #5
0
ファイル: ProcessEngine.cs プロジェクト: wenchun/NGinnBPM
        protected void UpdateTask(string instanceId, Action <TaskInstance> act)
        {
            RunProcessTransaction(this.DefaultPersistenceMode, ps =>
            {
                string ol = MappedDiagnosticsContext.Get("NG_TaskInstanceId");
                try
                {
                    MappedDiagnosticsContext.Set("NG_TaskInstanceId", instanceId);

                    var ti = ps.TaskPersister.GetForUpdate(instanceId);
                    if (ti == null)
                    {
                        log.Warn("Task instance not found: {0}", instanceId);
                        var pti = (CompositeTaskInstance)ps.TaskPersister.GetForRead(InstanceId.GetParentTaskInstanceId(instanceId));
                        if (pti == null)
                        {
                            throw new Exception("Task instance not found and no parent. Instance ID: " + instanceId);
                        }
                        var tin = pti.GetChildTransitionInfo(instanceId);
                        if (tin == null)
                        {
                            throw new Exception("Task instance not found anywhere: " + instanceId);
                        }
                        log.Info("Task instance not found, parent says that status is {0}", tin.Status);
                        return;
                    }
                    ;
                    var pd      = this.GetProcessDef(ti.ProcessDefinitionId);
                    var pscript = this.GetProcessScriptRuntime(ti.ProcessDefinitionId);
                    ti.Activate(ps, pd, pscript);
                    var pstat = ti.Status;
                    act(ti);
                    OnTaskInstanceStatusChange(ti, pstat);
                    ti.Deactivate();
                    ps.TaskPersister.Update(ti);
                }
                finally
                {
                    MappedDiagnosticsContext.Set("NG_TaskInstanceId", ol);
                }
            });
        }
コード例 #6
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(""));
        }
コード例 #7
0
 protected void UpdateTask(string instanceId, Action <TaskInstance> act)
 {
     RunProcessTransaction(this.DefaultPersistenceMode, ps =>
     {
         string ol = MappedDiagnosticsContext.Get("NG_TaskInstanceId");
         try
         {
             MappedDiagnosticsContext.Set("NG_TaskInstanceId", instanceId);
             var ti      = ps.PersisterSession.GetForUpdate(instanceId);
             var pd      = this.GetProcessDef(ti.ProcessDefinitionId);
             var pscript = this.GetProcessScriptRuntime(ti.ProcessDefinitionId);
             ti.Activate(ps, pd, pscript);
             act(ti);
             ti.Deactivate();
             ps.PersisterSession.Update(ti);
         }
         finally
         {
             MappedDiagnosticsContext.Set("NG_TaskInstanceId", ol);
         }
     });
 }
コード例 #8
0
        public void timer_cannot_inherit_mappedcontext()
        {
            object getObject = null;
            string getValue  = null;

            var   mre    = new ManualResetEvent(false);
            Timer thread = new Timer((s) =>
            {
                try
                {
                    getObject = MappedDiagnosticsContext.GetObject("DoNotExist");
                    getValue  = MappedDiagnosticsContext.Get("DoNotExistEither");
                }
                finally
                {
                    mre.Set();
                }
            });

            thread.Change(0, Timeout.Infinite);
            mre.WaitOne();
            Assert.Null(getObject);
            Assert.Empty(getValue);
        }
コード例 #9
0
        /// <summary>
        /// Renders the specified MDC item and appends it to the specified <see cref="StringBuilder" />.
        /// </summary>
        /// <param name="builder">The <see cref="StringBuilder"/> to append the rendered data to.</param>
        /// <param name="logEvent">Logging event.</param>
        protected override void Append(StringBuilder builder, LogEventInfo logEvent)
        {
            string msg = MappedDiagnosticsContext.Get(this.Item);

            builder.Append(msg);
        }
コード例 #10
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());
        }
コード例 #11
0
 /// <summary>
 ///   Gets or sets the value of a property
 /// </summary>
 /// <value>
 ///   The value for the property with the specified key
 /// </value>
 /// <remarks>
 ///   <para>
 ///     Gets or sets the value of a property
 ///   </para>
 /// </remarks>
 public object this[string key]
 {
     get { return(MappedDiagnosticsContext.Get(key)); }
     set { MappedDiagnosticsContext.Set(key, value); }
 }
コード例 #12
0
 /// <summary>
 /// Renders the specified MDC item and appends it to the specified <see cref="StringBuilder" />.
 /// </summary>
 /// <param name="builder">The <see cref="StringBuilder"/> to append the rendered data to.</param>
 /// <param name="logEvent">Logging event.</param>
 protected override void Append(StringBuilder builder, LogEventInfo logEvent)
 {
     builder.Append(MappedDiagnosticsContext.Get(this.Item, logEvent.FormatProvider));
 }