protected void Run(BaseDataMap map, LevelLogger logger, OutputHandlerBase exporter)
        {
            Context.Job.Status.State = JobState.Running;
            logger.AddKey(Utility.Constants.DataSyncItemId, map.ImportItem.ID.ToString());
            logger.AddData(Utility.Constants.DataSyncItem, map.ImportItem);
            logger.AddData(Logger.Log.Constants.Identifier, map.ImportItem.Name);
            var startedAt = DateTime.Now;

            logger = map.Process();
            var finishededAt = DateTime.Now;

            logger.AddData(Logger.Log.Constants.StartTime, startedAt);
            logger.AddData(Logger.Log.Constants.EndTime, finishededAt);
            try
            {
                MailManager.SendLogReport(ref logger, exporter);
            }
            catch (Exception exception)
            {
                Diagnostics.Log.Error(exporter.GetIdentifier() + " failed in sending out the mail. Please see the exception message for more details. Exception:" + exception.Message + ". Status:\r\n" +
                                      exporter.Export(), typeof(DataSyncWizard));
            }
            if (logger.HasErrors())
            {
                Context.Job.Status.Failed = true;
            }
            else
            {
                Context.Job.Status.State = JobState.Finished;
            }
        }
Exemplo n.º 2
0
        public static void SendLogReport(ref LevelLogger logger, OutputHandlerBase exporter)
        {
            var dataSyncItem = logger.GetData(Utility.Constants.DataSyncItem) as Item;

            if (dataSyncItem == null)
            {
                logger.AddError("Error", "The DataSyncItem was null. Therefor we couldn't retrieve any mail recipients.");
                return;
            }
            if (exporter == null)
            {
                logger.AddError("Error", "The ExporterBase was null. This class is used to output the logger.");
                return;
            }
            var recipient = dataSyncItem[FieldNameMailRecipients];

            if (String.IsNullOrEmpty(recipient))
            {
                logger.AddInfo("Add Mail Recipients to receive email reports", "If you want to receive email, then fill out the field 'Mail Recipients'.");
            }
            else
            {
                var replyTo = dataSyncItem[FieldNameMailReplyTo];
                if (String.IsNullOrEmpty(replyTo))
                {
                    replyTo = DefaultReplyTo;
                    logger.AddError("Error", "The 'Mail Reply To' field must be defined. Please provide a replyTo address for the mail.");
                }
                var subject = dataSyncItem[FieldNameMailSubject];
                if (String.IsNullOrEmpty(subject))
                {
                    subject = DefaultSubject;
                }

                var result = !logger.HasFatalsOrErrors() ? Success : Failure;
                try
                {
                    subject = String.Format(subject, exporter.GetIdentifier(), result);
                }
                catch (Exception exception)
                {
                    logger.AddError("Error", "SendLogReport had an exception trying to format the subject of the mail." + exception.Message);
                }
                var doNotSendMailOnSuccess = dataSyncItem[FieldNameDoNotSendMailOnSuccess] == SitecoreBooleanTrue;
                if ((doNotSendMailOnSuccess && result == Failure) || !doNotSendMailOnSuccess)
                {
                    try{
                        if (SendMail.SendMailWithoutAttachment(recipient, replyTo, subject, exporter.Export()) == Failure)
                        {
                            logger.AddError("Error", "The SendMailWithoutAttachment failed.");
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.AddError("Error", "The SendMailWithoutAttachment failed with an exception: " + ex.Message);
                    }
                }
            }
        }