Пример #1
0
        /// <summary>
        /// Performs one time initialization of the output target(s), e.g. open output file(s).
        /// This method is only called when the InitErrorOccurred property is accessed for the first time
        /// (which may not happen at all in case intake initialization fails).
        /// </summary>
        /// <returns>true if initialization successful; false if not, e.g. file not found</returns>
        private bool InitOutput()
        {
            // Note that in order for the exceptions to be caught in the try blocks below, CreateLineDispenser (or OutputInitializer as applicable) processing
            // must be eagerly evaluated (no deferred execution).
            if (_outputToWriter)
            {
                try
                {
                    //In case both OutputWriters and OutputFileNames are present, then OutputWriters wins
                    _outputFileWriter = _config.OutputWriters == null?LineDispenserCreator.CreateLineDispenser(_config.OutputFileNames.ToListOfStrings('|'), _config.OutputDataKind, _config.AsyncOutput, X12Delimiters == null?null : new Lazy <string>(() => X12Delimiters().X12SegmentDelimiter), _config.XmlJsonOutputSettings)
                                            : LineDispenserCreator.CreateLineDispenser(_config.OutputWriters(), _config.OutputDataKind, _config.AsyncOutput, X12Delimiters == null ? null : new Lazy <string>(() => X12Delimiters().X12SegmentDelimiter), _config.XmlJsonOutputSettings);

                    //Lazy here allows delimiters to be evaluated AFTER the first intake row (such as ISA segment), so that in absence of delimiters in config, they can be copied from intake
                }
                catch (Exception ex)
                {
                    //something went wrong with output file(s) or text writer(s), there is not much we can do other than to report it
                    var errMsg = _config.IntakeReaders == null ? $"Attempt to create output file(s) '{_config.OutputFileNames}' failed."
                                                          : "Attempt to open text writer(s) failed.";
                    _config.Logger.LogFatal(errMsg, ex);
                    return(false);
                }
                return(true);
            }
            else //output to OutputConsumer or (OutputConsumerAsync) action
            {
                string errMsg;
                try
                {
                    errMsg = _config.OutputInitializer(_globalCache);
                }
                catch (Exception ex)
                {
                    _config.Logger.LogFatal("Output initializer threw exception.", ex);
                    return(false);
                }
                if (errMsg != null)
                {
                    _config.Logger.LogFatal("Output initializer failed:\r\n" + errMsg);
                    return(false);
                }
                return(true);
            }
        }