Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added logging #52

Merged
merged 4 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added logging
  • Loading branch information
mvSapphire committed Dec 10, 2023
commit 309fdff4f93d26bb576e8b3c70cd3a0394e61d76
2 changes: 2 additions & 0 deletions samples/PowerPipe.Sample/PowerPipe.Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
</ItemGroup>

</Project>
8 changes: 8 additions & 0 deletions samples/PowerPipe.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using PowerPipe.Sample.Steps;

namespace PowerPipe.Sample;
Expand Down Expand Up @@ -30,6 +31,13 @@ private static IServiceProvider ConfigureServices()
.AddScoped(typeof(SampleGenericStep<>));
});

services.AddLogging(options =>
{
options
.AddConsole()
.SetMinimumLevel(LogLevel.Debug);
});

services.AddSingleton<SamplePipeline>();
var serviceProvider = services.BuildServiceProvider();

Expand Down
1 change: 0 additions & 1 deletion samples/PowerPipe.Sample/Steps/SampleGenericStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class SampleGenericStep<T>: IPipelineStep<SamplePipelineContext>

public async ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleGenericStep<T>)} Executed");
await NextStep.ExecuteAsync(context, cancellationToken);
}
}
2 changes: 0 additions & 2 deletions samples/PowerPipe.Sample/Steps/SampleParallelStep1.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using PowerPipe.Interfaces;
Expand All @@ -9,7 +8,6 @@ public class SampleParallelStep1 : IPipelineParallelStep<SamplePipelineContext>
{
public ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleParallelStep1)} Executed");
return ValueTask.CompletedTask;
}
}
2 changes: 0 additions & 2 deletions samples/PowerPipe.Sample/Steps/SampleParallelStep2.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using PowerPipe.Interfaces;
Expand All @@ -9,7 +8,6 @@ public class SampleParallelStep2 : IPipelineParallelStep<SamplePipelineContext>
{
public ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleParallelStep2)} Executed");
return ValueTask.CompletedTask;
}
}
2 changes: 0 additions & 2 deletions samples/PowerPipe.Sample/Steps/SampleParallelStep3.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using PowerPipe.Interfaces;
Expand All @@ -9,7 +8,6 @@ public class SampleParallelStep3 : IPipelineParallelStep<SamplePipelineContext>
{
public ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleParallelStep3)} Executed");
return ValueTask.CompletedTask;
}
}
2 changes: 0 additions & 2 deletions samples/PowerPipe.Sample/Steps/SampleParallelStep4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ public class SampleParallelStep4 : IPipelineParallelStep<SamplePipelineContext>
{
public ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleParallelStep4)} Executed");

throw new InvalidOperationException();

// return ValueTask.CompletedTask;
Expand Down
3 changes: 0 additions & 3 deletions samples/PowerPipe.Sample/Steps/SampleParallelStep5.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using PowerPipe.Interfaces;
Expand All @@ -9,8 +8,6 @@ public class SampleParallelStep5 : IPipelineParallelStep<SamplePipelineContext>
{
public ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleParallelStep5)} Executed");

// throw new InvalidOperationException();

return ValueTask.CompletedTask;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using PowerPipe.Interfaces;
Expand All @@ -9,7 +8,6 @@ public class SampleParallelStep5Compensation : IPipelineCompensationStep<SampleP
{
public ValueTask CompensateAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleParallelStep5Compensation)} Executed");
return ValueTask.CompletedTask;
}
}
2 changes: 0 additions & 2 deletions samples/PowerPipe.Sample/Steps/SampleParallelStep6.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using PowerPipe.Interfaces;
Expand All @@ -9,7 +8,6 @@ public class SampleParallelStep6 : IPipelineParallelStep<SamplePipelineContext>
{
public ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleParallelStep6)} Executed");
return ValueTask.CompletedTask;
}
}
2 changes: 0 additions & 2 deletions samples/PowerPipe.Sample/Steps/SampleParallelStep7.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using PowerPipe.Interfaces;
Expand All @@ -9,7 +8,6 @@ public class SampleParallelStep7 : IPipelineParallelStep<SamplePipelineContext>
{
public ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleParallelStep7)} Executed");
return ValueTask.CompletedTask;
}
}
2 changes: 0 additions & 2 deletions samples/PowerPipe.Sample/Steps/SampleStep1.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using PowerPipe.Interfaces;
Expand All @@ -10,7 +9,6 @@ public class SampleStep1 : IPipelineStep<SamplePipelineContext>
public IPipelineStep<SamplePipelineContext> NextStep { get; set; }
public async ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleStep1)} Executed");
await NextStep.ExecuteAsync(context, cancellationToken);
}
}
2 changes: 0 additions & 2 deletions samples/PowerPipe.Sample/Steps/SampleStep1Compensation.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using PowerPipe.Interfaces;
Expand All @@ -9,7 +8,6 @@ public class SampleStep1Compensation : IPipelineCompensationStep<SamplePipelineC
{
public ValueTask CompensateAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleStep1Compensation)} Executed");
return ValueTask.CompletedTask;
}
}
2 changes: 0 additions & 2 deletions samples/PowerPipe.Sample/Steps/SampleStep2.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using PowerPipe.Interfaces;
Expand All @@ -10,7 +9,6 @@ public class SampleStep2 : IPipelineStep<SamplePipelineContext>
public IPipelineStep<SamplePipelineContext> NextStep { get; set; }
public async ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleStep2)} Executed");
await NextStep.ExecuteAsync(context, cancellationToken);
}
}
2 changes: 0 additions & 2 deletions samples/PowerPipe.Sample/Steps/SampleStep2Compensation.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using PowerPipe.Interfaces;
Expand All @@ -9,7 +8,6 @@ public class SampleStep2Compensation : IPipelineCompensationStep<SamplePipelineC
{
public ValueTask CompensateAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleStep2Compensation)} Executed");
return ValueTask.CompletedTask;
}
}
2 changes: 0 additions & 2 deletions samples/PowerPipe.Sample/Steps/SampleStep3.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using PowerPipe.Interfaces;
Expand All @@ -10,7 +9,6 @@ public class SampleStep3 : IPipelineStep<SamplePipelineContext>
public IPipelineStep<SamplePipelineContext> NextStep { get; set; }
public async ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleStep3)} Executed");
await NextStep.ExecuteAsync(context, cancellationToken);
}
}
2 changes: 0 additions & 2 deletions samples/PowerPipe.Sample/Steps/SampleStep4.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using PowerPipe.Interfaces;
Expand All @@ -10,7 +9,6 @@ public class SampleStep4 : IPipelineStep<SamplePipelineContext>
public IPipelineStep<SamplePipelineContext> NextStep { get; set; }
public async ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleStep4)} Executed");
await NextStep.ExecuteAsync(context, cancellationToken);
}
}
2 changes: 0 additions & 2 deletions samples/PowerPipe.Sample/Steps/SampleStep5.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using PowerPipe.Interfaces;
Expand All @@ -10,7 +9,6 @@ public class SampleStep5 : IPipelineStep<SamplePipelineContext>
public IPipelineStep<SamplePipelineContext> NextStep { get; set; }
public async ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleStep5)} Executed");
await NextStep.ExecuteAsync(context, cancellationToken);
}
}
2 changes: 0 additions & 2 deletions samples/PowerPipe.Sample/Steps/SampleStep6.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using PowerPipe.Interfaces;
Expand All @@ -10,7 +9,6 @@ public class SampleStep6 : IPipelineStep<SamplePipelineContext>
public IPipelineStep<SamplePipelineContext> NextStep { get; set; }
public async ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleStep6)} Executed");
await NextStep.ExecuteAsync(context, cancellationToken);
}
}
3 changes: 0 additions & 3 deletions samples/PowerPipe.Sample/Steps/SampleStep7.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using PowerPipe.Interfaces;
Expand All @@ -10,8 +9,6 @@ public class SampleStep7 : IPipelineStep<SamplePipelineContext>
public IPipelineStep<SamplePipelineContext> NextStep { get; set; }
public async ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleStep7)} Executed");

// throw new InvalidOperationException();

await NextStep.ExecuteAsync(context, cancellationToken);
Expand Down
2 changes: 0 additions & 2 deletions samples/PowerPipe.Sample/Steps/SampleStep7Compensation.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using PowerPipe.Interfaces;
Expand All @@ -9,7 +8,6 @@ public class SampleStep7Compensation : IPipelineCompensationStep<SamplePipelineC
{
public ValueTask CompensateAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleStep7Compensation)} Executed");
return ValueTask.CompletedTask;
}
}
15 changes: 10 additions & 5 deletions src/PowerPipe/Builder/PipelineBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using PowerPipe.Builder.Steps;
using PowerPipe.Interfaces;

Expand All @@ -15,6 +16,7 @@ public sealed class PipelineBuilder<TContext, TResult>
where TResult : class
{
private readonly IPipelineStepFactory _pipelineStepFactory;
private readonly ILoggerFactory _loggerFactory;
private volatile TContext _context;

internal List<InternalStep<TContext>> Steps { get; } = new();
Expand All @@ -32,6 +34,7 @@ public PipelineBuilder(IPipelineStepFactory pipelineStepFactory, TContext contex

_pipelineStepFactory = pipelineStepFactory;
_context = context;
_loggerFactory = pipelineStepFactory.ServiceProvider.GetService(typeof(ILoggerFactory)) as ILoggerFactory;
}

/// <summary>
Expand All @@ -42,7 +45,7 @@ public PipelineBuilder(IPipelineStepFactory pipelineStepFactory, TContext contex
public PipelineBuilder<TContext, TResult> Add<T>()
where T : IStepBase<TContext>
{
Steps.Add(new LazyStep<TContext>(_pipelineStepFactory.Create<T, TContext>));
Steps.Add(new LazyStep<TContext>(_pipelineStepFactory.Create<T, TContext>, _loggerFactory));

return this;
}
Expand All @@ -56,7 +59,9 @@ public PipelineBuilder<TContext, TResult> Add<T>()
public PipelineBuilder<TContext, TResult> AddIf<T>(Predicate<TContext> predicate)
where T : IStepBase<TContext>
{
Steps.Add(new AddIfStep<TContext>(predicate, new LazyStep<TContext>(_pipelineStepFactory.Create<T, TContext>)));
Steps.Add(new AddIfStep<TContext>(
predicate,
new LazyStep<TContext>(_pipelineStepFactory.Create<T, TContext>, _loggerFactory)));

return this;
}
Expand All @@ -74,8 +79,8 @@ public PipelineBuilder<TContext, TResult> AddIfElse<TIf, TElse>(Predicate<TConte
{
Steps.Add(new AddIfElseStep<TContext>(
predicate,
new LazyStep<TContext>(_pipelineStepFactory.Create<TIf, TContext>),
new LazyStep<TContext>(_pipelineStepFactory.Create<TElse, TContext>)));
new LazyStep<TContext>(_pipelineStepFactory.Create<TIf, TContext>, _loggerFactory),
new LazyStep<TContext>(_pipelineStepFactory.Create<TElse, TContext>, _loggerFactory)));

return this;
}
Expand Down Expand Up @@ -151,4 +156,4 @@ public IPipeline<TResult> Build()

return new Pipeline<TContext, TResult>(_context, Steps);
}
}
}
6 changes: 6 additions & 0 deletions src/PowerPipe/Builder/Steps/InternalStep.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using PowerPipe.Exceptions;
using PowerPipe.Interfaces;

Expand All @@ -20,6 +21,11 @@ internal abstract class InternalStep<TContext> : IPipelineStep<TContext>, IPipel
/// </summary>
public CompensationStep<TContext> CompensationStep { get; set; }

/// <summary>
/// Gets or init a logger.
/// </summary>
protected ILogger Logger { get; set; }

/// <summary>
/// Gets or sets a value indicating whether this step has been executed.
/// </summary>
Expand Down
8 changes: 7 additions & 1 deletion src/PowerPipe/Builder/Steps/LazyStep.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using PowerPipe.Interfaces;

namespace PowerPipe.Builder.Steps;
Expand All @@ -17,7 +18,8 @@ internal class LazyStep<TContext> : InternalStep<TContext>
/// Initializes a new instance of the <see cref="LazyStep{TContext}"/> class.
/// </summary>
/// <param name="factory">A factory function to create the step when needed.</param>
internal LazyStep(Func<IStepBase<TContext>> factory)
/// <param name="loggerFactory">A logger factory</param>
internal LazyStep(Func<IStepBase<TContext>> factory, ILoggerFactory loggerFactory)
{
_step = new Lazy<IStepBase<TContext>>(() =>
{
Expand All @@ -26,6 +28,8 @@ internal LazyStep(Func<IStepBase<TContext>> factory)
if (instance is IPipelineStep<TContext> step)
step.NextStep = NextStep;

Logger = loggerFactory?.CreateLogger(instance.GetType());

return instance;
});
}
Expand All @@ -41,5 +45,7 @@ protected override async ValueTask ExecuteInternalAsync(TContext context, Cancel
StepExecuted = true;

await _step.Value.ExecuteAsync(context, cancellationToken);

Logger?.LogDebug("{Step} executed", _step.Value.GetType().FullName);
}
}
Loading
Loading