Files
ECMJobRunner/ECMJobRunner.Application/Profiles/Commands/Behaviors/JobExceptionHandlingBehavior.cs
TekH 111281ac08 feat(logging): improve exception handling and add comprehensive logging
JobExceptionHandlingBehavior:
- Add ILogger for diagnostic output
- Change ResultText to include full exception details (ToString())
- Log JobException with warning level including ProfileId, JobName, ProcessName, BatchId
- Return default instead of re-throwing to allow graceful handling

ReCRequestExecutionBehavior:
- Convert to primary constructor pattern
- Add ILogger for request tracking
- Store RecActionResult in command for later use
- Log successful ReC requests with detailed metrics (TotalActionCount, ActionExceptionCount)
- Improve error handling and logging
2026-08-04 16:33:53 +02:00

52 lines
2.2 KiB
C#

using ECMJobRunner.Application.Common.Exceptions;
using ECMJobRunner.Application.ProfileHistories.Commands;
using ECMJobRunner.Domain.ValueObjects;
using MediatR;
using Microsoft.Extensions.Logging;
using System.Threading;
using System.Threading.Tasks;
namespace ECMJobRunner.Application.Profiles.Commands.Behaviors;
/// <summary>
/// Pipeline behavior that catches <see cref="ECMJobRunner.Application.Common.Exceptions.JobException"/> exceptions,
/// persists a profile history error record and re-throws the exception
/// </summary>
/// <typeparam name="TRequest">The type of the MediatR request</typeparam>
/// <typeparam name="TResponse">The type of the MediatR response</typeparam>
/// <param name="Sender">MediatR sender used to dispatch the <see cref="ECMJobRunner.Application.ProfileHistories.Commands.CreateProfileHistoryCommand"/></param>
/// <param name="Logger">Logger for diagnostic output.</param>
public class JobExceptionHandlingBehavior<TRequest, TResponse>(ISender Sender, ILogger<JobExceptionHandlingBehavior<TRequest, TResponse>> Logger) : IPipelineBehavior<TRequest, TResponse> where TRequest : notnull
{
/// <summary>
/// Handles the pipeline behavior
/// Executes main query if request is TriggeringDEXJobCommand
/// </summary>
#if NET48
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
#else
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
#endif
{
try
{
return await next();
}
catch (JobException ex)
{
var cmd = new CreateProfileHistoryCommand()
{
ProfileId = ex.ProfileId,
Result = ResultType.Ok,
ResultText = ex.ToString(),
AddedWho = "ECMJobRunner"
};
await Sender.Send(cmd, cancellationToken);
Logger.LogWarning(ex, "JobException caught in JobExceptionHandlingBehavior for ProfileId {ProfileId}, JobName {JobName}, ProcessName {ProcessName}, BatchId {BatchId}", ex.ProfileId, ex.JobName, ex.ProcessName, ex.BatchId);
return default!;
}
}
}