Skip to content

Component Execution Status

Marty Mathis edited this page Aug 27, 2020 · 10 revisions

Pipeline Component Execution Status Monitoring

Pipeline Framework has some built in hooks you can extend to provide status feedback on the execution of each component within your pipeline. By implementing, IPipelineComponentExecutionStatusReceiver or IAsyncPipelineComponentExecutionStatusReceiver(depending on the type of pipeline you choose), you can receive status notifications for each executing component and handle those notifications as you see fit.

Example usage with Serilog:

using PipelineFramework.Abstractions;
using Serilog;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;

namespace PipelineFramework.Core.Examples
{
    [ExcludeFromCodeCoverage]
    public class ExecutionStatusReceiver : IAsyncPipelineComponentExecutionStatusReceiver
    {
        private readonly ILogger _logger;

        public ExecutionStatusReceiver(ILogger logger)
        {
            _logger = logger;
        }

        public Task ReceiveExecutionStartingAsync(PipelineComponentExecutionStartingInfo executionInfo)
        {
            var logger = EnrichLogger(_logger, executionInfo.Payload);
            logger.Information(
                "Component '{PipelineComponentName}' execution starting at {TimeStamp}",
                executionInfo.PipelineComponentName,
                executionInfo.TimeStamp.ToShortTimeString());

            return Task.CompletedTask;
        }

        public Task ReceiveExecutionCompletedAsync(PipelineComponentExecutionCompletedInfo executionInfo)
        {
            var logger = EnrichLogger(_logger, executionInfo.Payload);
            if (executionInfo.Exception == null)
            {
                logger.Information(
                       "Component '{PipelineComponentName}' execution completed at {TimeStamp}.  Duration: {TotalMilliseconds}ms",
                       executionInfo.PipelineComponentName,
                       executionInfo.TimeStamp.ToShortTimeString(),
                       executionInfo.ExecutionTime.TotalMilliseconds);
            }
            else
            {
                logger.Error(
                    executionInfo.Exception,
                    "Component '{PipelineComponentName}' execution failed at {TimeStamp}.  Duration: {TotalMilliseconds}ms",
                    executionInfo.PipelineComponentName,
                    executionInfo.TimeStamp.ToShortTimeString(),
                    executionInfo.ExecutionTime.TotalMilliseconds);
            }

            return Task.CompletedTask;
        }

        private static ILogger EnrichLogger(ILogger logger, object payload) =>
            payload is ExamplePipelinePayload p
                ? logger.ForContext("FooKey", p.FooKey).ForContext("Result", p.Result)
                : logger;
    }
}
Clone this wiki locally