Skip to content

Commit

Permalink
Additional extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
justdmitry committed Jan 21, 2022
1 parent c0f2e77 commit 0ec07a9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/RecurrentTasks/TaskOptions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace RecurrentTasks
{
using Microsoft.Extensions.Logging;
using System;
using System.Globalization;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

public class TaskOptions
{
Expand All @@ -15,6 +15,9 @@ public class TaskOptions
private TimeSpan interval;
private TimeSpan firstRunDelay = TimeSpan.FromSeconds(new Random().Next(10, 30));

/// <summary>
/// Custom logger (to use instead of calling loggerFactory.CreateLogger()).
/// </summary>
public ILogger Logger { get; set; }

/// <summary>
Expand Down
20 changes: 20 additions & 0 deletions src/RecurrentTasks/TaskOptionsExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace Microsoft.Extensions.DependencyInjection
{
using System;
using System.Globalization;
using Microsoft.Extensions.Logging;
using RecurrentTasks;

public static class TaskOptionsExtensions
Expand All @@ -27,5 +29,23 @@ public static TaskOptions AutoStart(this TaskOptions taskOptions, uint interval,
{
return AutoStart(taskOptions, TimeSpan.FromSeconds(interval), TimeSpan.FromSeconds(firstRunDelay));
}

/// <summary>
/// Sets <see cref="TaskOptions.Logger"/> (custom logger to use instead of calling loggerFactory.CreateLogger()).
/// </summary>
public static TaskOptions WithLogger(this TaskOptions taskOptions, ILogger logger)
{
taskOptions.Logger = logger;
return taskOptions;
}

/// <summary>
/// Sets <see cref="TaskOptions.RunCulture"/>, that will be set before <see cref="IRunnable.RunAsync"/> is called.
/// </summary>
public static TaskOptions WithCulture(this TaskOptions taskOptions, CultureInfo culture)
{
taskOptions.RunCulture = culture;
return taskOptions;
}
}
}

0 comments on commit 0ec07a9

Please sign in to comment.