private void AddExecutionResults(IJobExecutionContext context, Exception exception, string message, bool logicalError = false) { var internalDetail = _internalDetails.FirstOrDefault(dt => dt.JobKey.Equals(context.JobDetail.Key)); if (internalDetail == null) { throw new ArgumentException($"Invalid job key: {context.JobDetail.Key}"); } string historyMessage; if (exception != null) { var allMesages = exception.GetAllMessages(); historyMessage = String.Join(Environment.NewLine, allMesages); } else { historyMessage = String.IsNullOrEmpty(message) ? "Execution was successful!" : message; } var ehd = new ExecutionHistoryDetails { ID = Guid.NewGuid(), JobName = context.JobDetail.Description, JobInstance = internalDetail.JobInstanceAsJson, JobType = context.JobDetail.JobType.FullName, JobParameters = context.MergedJobDataMap.ToDictionary(k => k.Key, v => v.Value), ScheduledAt = context.ScheduledFireTimeUtc?.LocalDateTime, ExecutedAt = context.FireTimeUtc.LocalDateTime, ExecutionTime = context.JobRunTime, ExecutionFailed = logicalError || (exception != null), ExceptionThrown = exception.AsJson(), ExecutionMessage = historyMessage, ExecutionOnDemand = (context.MergedJobDataMap?.GetString(OnDemandKey) ?? String.Empty) == true.ToString() }; _historyManager.Save(ehd); }