Esempio n. 1
0
        private static int Main(string[] args)
        {
            try
            {
                // Accepting command-line arguments
                var parsedArgs = new ParsedArguments {
                    RenderInfo = new RenderInfo()
                };
                string errorMessage;
                var    success = AcceptArguments(args, ref parsedArgs, out errorMessage);
                if (!success)
                {
                    Log(DefaultWriter, errorMessage);
                    return(1);
                }

                var renderFactory = new PlogRenderFactory(parsedArgs);

                var acceptedRenderTypes = parsedArgs.RenderTypes != null && parsedArgs.RenderTypes.Count > 0
                    ? parsedArgs.RenderTypes.ToArray()
                    : Utils.GetEnumValues <RenderType>();
                var renderTasks = new Task[acceptedRenderTypes.Length];
                for (var index = 0; index < renderTasks.Length; index++)
                {
                    var closureIndex = index;
                    renderTasks[index] =
                        Task.Factory.StartNew(
                            () =>
                            renderFactory.GetRenderService(acceptedRenderTypes[closureIndex],
                                                           (renderType, path) =>
                                                           DefaultWriter.WriteLine("{0} output was saved to {1}",
                                                                                   Enum.GetName(typeof(RenderType), renderType), path)).Render());
                }

                Task.WaitAll(renderTasks);
                return(0);
            }
            catch (AggregateException aggrEx)
            {
                var baseEx = aggrEx.GetBaseException();
                Log(DefaultWriter, baseEx.ToString());

                return(1);
            }
            catch (Exception ex)
            {
                Log(DefaultWriter, ex.ToString());

                return(1);
            }
        }
        private static int Main(string[] args)
        {
            try
            {
                // Accepting command-line arguments
                var parsedArgs = new ParsedArguments {
                    RenderInfo = new RenderInfo()
                };
                var success = AcceptArguments(args, ref parsedArgs, out string errorMessage, out IList <string> warningMessages);
                if (!success)
                {
                    ErrortWriter.WriteLine(errorMessage);
                    return((int)ConverterRunState.IncorrectArguments);
                }
                else if (!string.IsNullOrWhiteSpace(errorMessage))
                {
                    DefaultWriter.WriteLine(errorMessage);
                    return((int)ConverterRunState.Success);
                }

                if (warningMessages.Any())
                {
                    warningMessages.ToList().ForEach(msg => DefaultWriter.WriteLine(msg));
                }

                var renderFactory = new PlogRenderFactory(parsedArgs, Logger);

                var acceptedRenderTypes = parsedArgs.RenderTypes != null && parsedArgs.RenderTypes.Count > 0
                    ? parsedArgs.RenderTypes.ToArray()
                    : Utils.GetEnumValues <LogRenderType>();

                ConcurrentBag <int> filteredErrorsByRenderTypes = new ConcurrentBag <int>();

                var renderTasks = new Task[acceptedRenderTypes.Length];
                for (var index = 0; index < renderTasks.Length; index++)
                {
                    var closureIndex = index;
                    renderTasks[index] =
                        Task.Factory.StartNew(() =>
                    {
                        var plogRenderer = renderFactory.GetRenderService(acceptedRenderTypes[closureIndex],
                                                                          (renderType, path) =>
                                                                          DefaultWriter.WriteLine("{0} output was saved to {1}",
                                                                                                  Enum.GetName(typeof(LogRenderType), renderType),
                                                                                                  path));

                        filteredErrorsByRenderTypes.Add(plogRenderer.Errors.Count());
                        plogRenderer.Render();
                    });
                }

                Task.WaitAll(renderTasks);

                if (renderFactory.Logger != null && renderFactory.Logger.ErrorCode != 0)
                {
                    return((int)ConverterRunState.RenderException);
                }

                return((int)((parsedArgs.IndicateWarnings &&
                              filteredErrorsByRenderTypes.Any(errorsByRenderType => errorsByRenderType != 0)) ? ConverterRunState.OutputLogNotEmpty
                                                                                                                 : ConverterRunState.Success));
            }
            catch (AggregateException aggrEx)
            {
                var baseEx = aggrEx.GetBaseException();
                Logger.LogError(baseEx.ToString());
                return((int)ConverterRunState.GeneralException);
            }
            catch (Exception ex)
            {
                if (ex is IOException ||
                    ex is UnauthorizedAccessException ||
                    ex is SettingsLoadException)
                {
                    Logger.LogError(ex.Message);
                }
                else
                {
                    Logger.LogError(ex.ToString());
                }

                return((int)ConverterRunState.GeneralException);
            }
        }