public static OutputHandlerBase CreateOutputHandler(Item dataSyncItem, LevelLogger logger)
        {
            string outHandlerAssembly = dataSyncItem[FieldNameOutputHandlerAssembly];
            string outputHandlerClass = dataSyncItem[FieldNameOutputHandlerClass];

            if (!String.IsNullOrEmpty(outHandlerAssembly))
            {
                if (!String.IsNullOrEmpty(outputHandlerClass))
                {
                    OutputHandlerBase outputHandler = null;
                    try
                    {
                        outputHandler = (OutputHandlerBase)Reflection.ReflectionUtil.CreateObject(outHandlerAssembly, outputHandlerClass, new object[] { logger });
                    }
                    catch (FileNotFoundException fnfe)
                    {
                        throw new Exception("The binary specified could not be found" + fnfe.Message, fnfe);
                    }
                    if (outputHandler != null)
                    {
                        return(outputHandler);
                    }
                    throw new Exception(String.Format("The OutputHandler provided could not be instantiated. Assembly:'{0}' Class:'{1}'", outHandlerAssembly, outputHandlerClass));
                }
                else
                {
                    throw new Exception(String.Format("OutputHandler class is not defined"));
                }
            }
            else
            {
                throw new Exception("OutputHandler assembly is not defined");
            }
            return(null);
        }
        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;
            }
        }
Example #3
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);
                    }
                }
            }
        }
        protected void StartImport()
        {
            var dataSyncItem = GetDataSyncItem();

            var               dataSyncManager = new DataSyncManager();
            LevelLogger       logger          = Manager.CreateLogger(dataSyncItem);
            OutputHandlerBase exporter        = Manager.CreateOutputHandler(dataSyncItem, logger);

            logger.AddKey(Utility.Constants.DataSyncItemId, dataSyncItem.ID.ToString());
            logger.AddData(Utility.Constants.DataSyncItem, dataSyncItem);
            var map = dataSyncManager.InstantiateDataMap(dataSyncItem, ref logger);

            if (map != null)
            {
                if (!String.IsNullOrEmpty(DataValue))
                {
                    map.Data = DataValue;
                }
                var options = new JobOptions("FromFileDataSyncWizard", "Job category name", Context.Site.Name, new FromFileDataSyncWizard(), "Run", new object[] { map, map.Logger, exporter });
                var job     = JobManager.Start(options);
                job.Options.CustomData = map.Logger;
                JobHandle = job.Handle.ToString();
                SheerResponse.Timer("CheckStatus", 5);
            }
            else
            {
                Active = "LastPage";
                BackButton.Disabled = true;
                if (logger.HasErrorsOrInfos())
                {
                    if (exporter != null)
                    {
                        ResultText.Value = exporter.Export();
                    }
                    else
                    {
                        ResultText.Value = "The Exporter class was null. Therefor the log was not written out.";
                    }
                }
            }
        }
Example #5
0
        public OutputDispatch(ILogger <OutputDispatch> logger, IServiceProvider serviceProvider, IConfiguration outputConfig)
        {
            _logger       = logger;
            _outputConfig = outputConfig;

            var type          = outputConfig.GetValue <string>("Type");
            var configSection = outputConfig.GetSection("Config");

            _logFilter        = FilterConfigurationFactory.Create(outputConfig.GetSection("filter"), AllMatchFilter.Default);
            _logExcludeFilter = FilterConfigurationFactory.Create(outputConfig.GetSection("excludeFilter"), NoneMatchFilter.Default);

            var outputFactory = serviceProvider.GetServices <IOutputFactory>().FirstOrDefault(x => x.ShortTypeName == type);

            if (outputFactory == null)
            {
                // TODO bessere Exception?
                throw new ArgumentException($"Unknown output type '{outputConfig.GetValue<string>("Type")}'.");
            }

            _output = outputFactory.Create(serviceProvider, configSection);
            _logger.LogInformation("Loaded output '{type}' with filter '{filter}' and exclude '{excludeFilter}'.", type, _logFilter, _logExcludeFilter);
        }