1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-01-31 00:19:00 +03:00
Octobot/TeamOctolings.Octobot/Extensions/LoggerExtensions.cs
Octol1ttle e457b4609e
Don't log stack traces for cancelled operations (#327)
This PR fixes an issue where the `LogResultStackTrace` method would log
stack traces for results that encountered an error due to a cancelled
operation/task. The `LoggerExtensions` class already skipped
`TaskCanceledException`s, but didn't skip `OperationCanceledException`s
(which is a parent of `TaskCanceledException`).

The patch specifically does not affect *inner* results which are
canceled. Skipping logging these could hide the true cause of an error
which appears important
2024-07-31 23:57:21 +05:00

40 lines
1.4 KiB
C#

using Microsoft.Extensions.Logging;
using Remora.Results;
namespace TeamOctolings.Octobot.Extensions;
public static class LoggerExtensions
{
/// <summary>
/// Checks if the <paramref name="result" /> has failed due to an error that has resulted from neither invalid user
/// input nor the execution environment and logs the error using the provided <paramref name="logger" />.
/// </summary>
/// <remarks>
/// This has special behavior for <see cref="ExceptionError" /> - its exception will be passed to the
/// <paramref name="logger" />
/// </remarks>
/// <param name="logger">The logger to use.</param>
/// <param name="result">The Result whose error check.</param>
/// <param name="message">The message to use if this result has failed.</param>
public static void LogResult(this ILogger logger, IResult result, string? message = "")
{
if (result.IsSuccess)
{
return;
}
if (result.Error is ExceptionError exe)
{
if (exe.Exception is OperationCanceledException)
{
return;
}
logger.LogError(exe.Exception, "{ErrorMessage}", message);
return;
}
logger.LogWarning("{UserMessage}{NewLine}{ResultErrorMessage}", message, Environment.NewLine,
result.Error.Message);
}
}