diff --git a/src/MR.AspNetCore.Jobs.SqlServer/JobsSqlServerLoggerExtensions.cs b/src/MR.AspNetCore.Jobs.SqlServer/LoggerExtensions.cs similarity index 89% rename from src/MR.AspNetCore.Jobs.SqlServer/JobsSqlServerLoggerExtensions.cs rename to src/MR.AspNetCore.Jobs.SqlServer/LoggerExtensions.cs index 63a847a..ba17c7d 100644 --- a/src/MR.AspNetCore.Jobs.SqlServer/JobsSqlServerLoggerExtensions.cs +++ b/src/MR.AspNetCore.Jobs.SqlServer/LoggerExtensions.cs @@ -3,7 +3,7 @@ namespace MR.AspNetCore.Jobs { - internal static class JobsSqlServerLoggerExtensions + internal static class LoggerExtensions { private static Action _collectingExpiredEntities; @@ -11,7 +11,7 @@ internal static class JobsSqlServerLoggerExtensions private static Action _installingError; private static Action _installingSuccess; - static JobsSqlServerLoggerExtensions() + static LoggerExtensions() { _collectingExpiredEntities = LoggerMessage.Define( LogLevel.Debug, @@ -44,9 +44,9 @@ public static void Installing(this ILogger logger) _installing(logger, null); } - public static void InstallingError(this ILogger logger, Exception exception) + public static void InstallingError(this ILogger logger, Exception ex) { - _installingError(logger, exception); + _installingError(logger, ex); } public static void InstallingSuccess(this ILogger logger) diff --git a/src/MR.AspNetCore.Jobs.SqlServer/MR.AspNetCore.Jobs.SqlServer.csproj b/src/MR.AspNetCore.Jobs.SqlServer/MR.AspNetCore.Jobs.SqlServer.csproj index 8ba7a3c..751af00 100644 --- a/src/MR.AspNetCore.Jobs.SqlServer/MR.AspNetCore.Jobs.SqlServer.csproj +++ b/src/MR.AspNetCore.Jobs.SqlServer/MR.AspNetCore.Jobs.SqlServer.csproj @@ -11,6 +11,7 @@ aspnetcore;background;jobs;sql;sqlserver https://github.com/mrahhal/MR.AspNetCore.Jobs https://github.com/mrahhal/MR.AspNetCore.Jobs/blob/master/LICENSE.txt + MR.AspNetCore.Jobs diff --git a/src/MR.AspNetCore.Jobs/JobsLoggerExtensions.cs b/src/MR.AspNetCore.Jobs/LoggerExtensions.cs similarity index 72% rename from src/MR.AspNetCore.Jobs/JobsLoggerExtensions.cs rename to src/MR.AspNetCore.Jobs/LoggerExtensions.cs index 081c338..bf5ef6b 100644 --- a/src/MR.AspNetCore.Jobs/JobsLoggerExtensions.cs +++ b/src/MR.AspNetCore.Jobs/LoggerExtensions.cs @@ -6,7 +6,7 @@ namespace MR.AspNetCore.Jobs { - internal static class JobsLoggerExtensions + internal static class LoggerExtensions { private static Action _serverStarting; private static Action _serverShuttingDown; @@ -20,9 +20,11 @@ internal static class JobsLoggerExtensions private static Action _jobFailed; private static Action _jobFailedWillRetry; private static Action _jobExecuted; - private static Action _jobRetrying; + private static Action _jobRetrying; + private static Action _jobCouldNotBeLoaded; + private static Action _exceptionOccuredWhileExecutingJob; - static JobsLoggerExtensions() + static LoggerExtensions() { _serverStarting = LoggerMessage.Define( LogLevel.Debug, @@ -69,15 +71,26 @@ static JobsLoggerExtensions() 2, "Job failed to execute. Will retry."); - _jobRetrying = LoggerMessage.Define( + _jobRetrying = LoggerMessage.Define( LogLevel.Debug, 3, - "Job Retrying."); + "Retrying a job: {Retries}..."); _jobExecuted = LoggerMessage.Define( LogLevel.Debug, 4, "Job executed. Took: {Seconds} secs."); + + _jobCouldNotBeLoaded = LoggerMessage.Define( + LogLevel.Warning, + 5, + "Could not load a job: '{JobId}'."); + + _exceptionOccuredWhileExecutingJob = LoggerMessage.Define( + LogLevel.Error, + 6, + "An exception occured while trying to execute a job: '{JobId}'. " + + "Requeuing for another retry."); } public static void ServerStarting(this ILogger logger, int machineProcessorCount, int processorCount) @@ -90,9 +103,9 @@ public static void ServerShuttingDown(this ILogger logger) _serverShuttingDown(logger, null); } - public static void ExpectedOperationCanceledException(this ILogger logger, Exception exception) + public static void ExpectedOperationCanceledException(this ILogger logger, Exception ex) { - _expectedOperationCanceledException(logger, exception.Message, exception); + _expectedOperationCanceledException(logger, ex.Message, ex); } public static void CronJobsNotFound(this ILogger logger) @@ -110,29 +123,39 @@ public static void CronJobExecuted(this ILogger logger, string name, double seco _cronJobExecuted(logger, name, seconds, null); } - public static void CronJobFailed(this ILogger logger, string name, Exception exception) + public static void CronJobFailed(this ILogger logger, string name, Exception ex) { - _cronJobFailed(logger, name, exception); + _cronJobFailed(logger, name, ex); } - public static void JobFailed(this ILogger logger, Exception exception) + public static void JobFailed(this ILogger logger, Exception ex) { - _jobFailed(logger, exception); + _jobFailed(logger, ex); } - public static void JobFailedWillRetry(this ILogger logger, Exception exception) + public static void JobFailedWillRetry(this ILogger logger, Exception ex) { - _jobFailedWillRetry(logger, exception); + _jobFailedWillRetry(logger, ex); } - public static void JobRetrying(this ILogger logger) + public static void JobRetrying(this ILogger logger, int retries) { - _jobRetrying(logger, null); + _jobRetrying(logger, retries, null); } public static void JobExecuted(this ILogger logger, double seconds) { _jobExecuted(logger, seconds, null); } + + public static void JobCouldNotBeLoaded(this ILogger logger, int jobId, Exception ex) + { + _jobCouldNotBeLoaded(logger, jobId, ex); + } + + public static void ExceptionOccuredWhileExecutingJob(this ILogger logger, int jobId, Exception ex) + { + _exceptionOccuredWhileExecutingJob(logger, jobId, ex); + } } } diff --git a/src/MR.AspNetCore.Jobs/Server/IProcessor.DelayedJob.cs b/src/MR.AspNetCore.Jobs/Server/IProcessor.DelayedJob.cs index b08ecc1..6e4f5b3 100644 --- a/src/MR.AspNetCore.Jobs/Server/IProcessor.DelayedJob.cs +++ b/src/MR.AspNetCore.Jobs/Server/IProcessor.DelayedJob.cs @@ -97,8 +97,7 @@ private async Task Step(ProcessingContext context) if (job.Retries > 0) { - _logger.LogDebug( - $"Retrying a job: {job.Retries}..."); + _logger.JobRetrying(job.Retries); } var result = await ExecuteJob(method, instance); @@ -134,22 +133,14 @@ private async Task Step(ProcessingContext context) } catch (JobLoadException ex) { - _logger.LogWarning( - 5, - ex, - "Could not load a job: '{JobId}'.", - job.Id); + _logger.JobCouldNotBeLoaded(job.Id, ex); await _stateChanger.ChangeStateAsync(job, new FailedState(), connection); fetched.RemoveFromQueue(); } catch (Exception ex) { - _logger.LogWarning( - 6, - ex, - "An exception occured while trying to execute a job: '{JobId}'. Requeuing for another retry.", - job.Id); + _logger.ExceptionOccuredWhileExecutingJob(job.Id, ex); fetched.Requeue(); }