public static string GetContext() { if (!MappedDiagnosticsContext.Contains(TagologContextParamName)) { return(string.Empty); } return(MappedDiagnosticsContext.Get(TagologContextParamName)); }
private Command GetCurrentCommand() { var commandId = MappedDiagnosticsContext.Get("CommandId"); if (string.IsNullOrWhiteSpace(commandId)) { return(null); } return(_trackCommands.GetById(commandId)); }
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); }
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); } } }
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); } }); }
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("")); }
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); } }); }
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); }
/// <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); }
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()); }
/// <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); } }
/// <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)); }