protected override TAdapterOutput RunTask(TaskContext context, TInput input, ILogger logger) { if (this.m_LogFactory == null) { return((TAdapterOutput) new Null()); } object[] parameters = input.MessageParams; if (parameters == null) { parameters = new object[1] { "" } } ; LoggingContext loggingContext = input.LoggingContext; string logTraceSourceName = this.GetConfiguration(input.Orchestration).LogTraceSourceName; if (String.IsNullOrEmpty(logTraceSourceName)) { if (!loggingContext.LoggingScopes.TryGetValue("LogTraceSourceName", out logTraceSourceName)) { logTraceSourceName = this.GetType().Namespace + "." + this.GetType().Name; } } ILogManager logManager = new LogManager(m_LogFactory, logTraceSourceName); foreach (var scope in loggingContext.LoggingScopes) { logManager.AddScope(scope.Key, scope.Value); } // Add WorkflowInstanceId as a new ActivityId if not existing yet. if (!logManager.CurrentScope.ContainsKey("OrchestrationInstanceId")) { logManager.AddScope("OrchestrationInstanceId", context.OrchestrationInstance.InstanceId); } switch (input.LoggingAction) { case Entities.Action.TraceInfo: logManager.TraceMessage(input.TracingLevel, input.EventId, input.FormatedMessage, parameters); break; case Entities.Action.TraceWarning: logManager.TraceWarning(input.TracingLevel, input.EventId, input.FormatedMessage, parameters); break; case Entities.Action.TraceError: if (input.Exception != null) { logManager.TraceError(input.TracingLevel, input.EventId, input.Exception, input.FormatedMessage, parameters); } else { logManager.TraceError(input.TracingLevel, input.EventId, input.FormatedMessage, parameters); } break; } return((TAdapterOutput) new Null()); } }
protected override LoggingContext RunTask(TaskContext context, PrepareLoggingContextTaskInput input) { if (String.IsNullOrEmpty(input.LogTraceSourceName) == false) { this.GetConfiguration(input.Orchestration).LogTraceSourceName = input.LogTraceSourceName; } if (String.IsNullOrEmpty(this.GetConfiguration(input.Context["Orchestration"]).LogTraceSourceName)) { this.GetConfiguration(input.Orchestration).LogTraceSourceName = this.GetType().Namespace + "." + this.GetType().Name; } LoggingContext parentLoggingContext = input.ParentLoggingContext; string traceSourceName = this.GetConfiguration(input.Orchestration).LogTraceSourceName; ILogManager parentScope = new LogManager(input.LoggerFactory, traceSourceName); parentScope.AddScope("ParentSequenceId", null); if (parentLoggingContext != null && parentLoggingContext.LoggingScopes != null && parentLoggingContext.LoggingScopes.Count > 0) { foreach (var scope in parentLoggingContext.LoggingScopes) { parentScope.AddScope(scope.Key, scope.Value); } // Copy the previous SequenceId to ParentSequenceId to keep the track. if (parentScope.CurrentScope.ContainsKey("SequenceId")) { parentScope.AddScope("ParentSequenceId", parentScope.CurrentScope["SequenceId"]); } } LogManager logManager = new LogManager(input.LoggerFactory, traceSourceName); // With new instance of the LogManager we always create a new SequenceId. logManager.AddScope("SequenceId", Guid.NewGuid().ToString()); // Add an new ActivityId if not existing yet. if (!logManager.CurrentScope.ContainsKey("ActivityId")) { logManager.AddScope("ActivityId", Guid.NewGuid().ToString()); } // Add WorkflowInstanceId as a new ActivityId if not existing yet. if (!logManager.CurrentScope.ContainsKey("OrchestrationInstanceId")) { logManager.AddScope("OrchestrationInstanceId", context.OrchestrationInstance.InstanceId); } logManager.AddScope("LogTraceSourceName", this.GetConfiguration(input.Orchestration).LogTraceSourceName); lock (m_TraceSourceInitialized) { // All DALs will append its class name which will identify them as component. //m_Log.AddScope(ApiBase.CreateComponentScopeName() + ".dal", this.GetType().Name); if (!m_TraceSourceInitialized.Contains(traceSourceName)) { m_TraceSourceInitialized.Add(traceSourceName); } // logManager.TraceMessage(TracingLevel.Level4, 0, "Logging Context prepared for the workflow instance with the Id=" + context.WorkflowInstanceId.ToString()); } var newLoggingContext = new LoggingContext { LoggingScopes = logManager.CurrentScope }; // copyScopes(input.ParentOrchestrationInput, newLoggingContext); return(newLoggingContext); }
//private static List<string> m_TraceSourceInitialized = new List<string>(); private void initializeLog(TaskContext context, TaskInput inputArg) { if (m_Log == null) { string logTraceSourceName = null; ILogManager parentLogMgr = new LogManager(inputArg.LoggerFactory, "not-used"); LoggingContext parentLoggingContext = null; if (inputArg.Context.ContainsKey("ParentLoggingContext")) { parentLoggingContext = inputArg.Context["ParentLoggingContext"] as LoggingContext; if (parentLoggingContext?.LoggingScopes != null) { foreach (var scope in parentLoggingContext.LoggingScopes) { // If Log Trace Source name is in parent context it will be used. if (scope.Key == "LogTraceSourceName") { logTraceSourceName = scope.Value; } parentLogMgr.AddScope(scope.Key, scope.Value); } // Copy the previous SequenceId to ParentSequenceId to keep the track. if (parentLogMgr.CurrentScope.ContainsKey("SequenceId")) { parentLogMgr.AddScope("ParentSequenceId", parentLogMgr.CurrentScope["SequenceId"]); } else { parentLogMgr.AddScope("ParentSequenceId", null); } } } if (parentLoggingContext == null) { parentLoggingContext = new LoggingContext(); } // // If log trace source name is specified in the configuration it will be used even if the context contains a parent logtrace source name. var cfg = this.GetConfiguration(inputArg.Orchestration); if (cfg != null) { logTraceSourceName = cfg.LogTraceSourceName; } if (String.IsNullOrEmpty(logTraceSourceName)) { logTraceSourceName = this.GetType().FullName; } m_Log = new LogManager(inputArg.LoggerFactory, logTraceSourceName, parentLogMgr); // With new instance of the LogManager we always create a new SequenceId. m_Log.AddScope("SequenceId", Guid.NewGuid().ToString()); // Add an new ActivityId if not existing yet. if (!m_Log.CurrentScope.ContainsKey("ActivityId")) { m_Log.AddScope("ActivityId", Guid.NewGuid().ToString()); } // Add OrchestrationInstanceId if (!m_Log.CurrentScope.ContainsKey("OrchestrationInstanceId")) { m_Log.AddScope("OrchestrationInstanceId", context.OrchestrationInstance.InstanceId); } } }