Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit a2cc5e8

Browse files
committed
Add logging for Forget(this Task task)
1 parent bbcba37 commit a2cc5e8

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

src/GitHub.Extensions/TaskExtensions.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
using System;
22
using System.Threading.Tasks;
3+
using GitHub.Logging;
34
using Serilog;
45

56
namespace GitHub.Extensions
67
{
78
public static class TaskExtensions
89
{
10+
static readonly ILogger log = LogManager.ForContext(typeof(TaskExtensions));
11+
912
public static async Task<T> Catch<T>(this Task<T> source, Func<Exception, T> handler = null)
1013
{
1114
Guard.ArgumentNotNull(source, nameof(source));
@@ -37,23 +40,35 @@ public static async Task Catch(this Task source, Action<Exception> handler = nul
3740
}
3841
}
3942

40-
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "task")]
41-
public static void Forget(this Task task)
43+
/// <summary>
44+
/// Allow task to run and log any exceptions.
45+
/// </summary>
46+
/// <param name="task">The <see cref="Task"/> to log exceptions from.</param>
47+
/// <param name="errorMessage">An error message to log if the task throws.</param>
48+
public static void Forget(this Task task, string errorMessage = "")
4249
{
50+
task.ContinueWith(t =>
51+
{
52+
if (t.IsFaulted)
53+
{
54+
log.Error(t.Exception, errorMessage);
55+
}
56+
});
4357
}
4458

4559
/// <summary>
46-
/// Log any exceptions when a task throws.
60+
/// Allow task to run and log any exceptions.
4761
/// </summary>
48-
/// <param name="task">The <see cref="Task"/> to log exceptions from.</param>
49-
/// <param name="log">The <see cref="ILogger"/> to use.</param>
50-
public static void Forget(this Task task, ILogger log)
62+
/// <param name="task">The task to log exceptions from.</param>
63+
/// <param name="log">The logger to use.</param>
64+
/// <param name="errorMessage">The error message to log if the task throws.</param>
65+
public static void Forget(this Task task, ILogger log, string errorMessage = "")
5166
{
5267
task.ContinueWith(t =>
5368
{
5469
if (t.IsFaulted)
5570
{
56-
log.Error(t.Exception, nameof(Forget));
71+
log.Error(t.Exception, errorMessage);
5772
}
5873
});
5974
}

0 commit comments

Comments
 (0)