Exemplo n.º 1
0
        private static ILogger SetUpLogger(int appendersCnt, IReader reader, IWriter writer, IFile file)
        {
            ICollection <IAppender> appenders = new HashSet <IAppender>();

            for (int i = 0; i < appendersCnt; i++)
            {
                string[] appendersArgs = reader.ReadLine()
                                         .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                string appenderType = appendersArgs[0];
                string layoutType   = appendersArgs[1];

                bool hasError = false;

                Level level = ParseLevel(appendersArgs, writer, ref hasError);

                if (hasError)
                {
                    continue;
                }

                try
                {
                    ILayout   layout   = layoutFactory.CreateLayout(layoutType);
                    IAppender appender = appenderFactory.CreateAppender(appenderType, layout, level, file);

                    appenders.Add(appender);
                }
                catch (InvalidOperationException ioe)
                {
                    writer.WriteLine(ioe.Message);
                }
            }

            ILogger logger = new MyLogger(appenders);

            return(logger);
        }
Exemplo n.º 2
0
        private static ILogger CreateLogger(int appendersCount, IReader reader, IWriter writer, IFile file, LayoutFactory layoutFactory, AppenderFactory appenderFactory)
        {
            ICollection <IAppender> appenders = new List <IAppender>();

            for (int i = 0; i < appendersCount; i++)
            {
                string[] appenderInfo     = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
                string   appenderType     = appenderInfo[0];
                string   layoutType       = appenderInfo[1];
                Level    appenderTreshold = Level.INFO;
                if (appenderInfo.Length == 3)
                {
                    bool isValidLevel = Enum.TryParse(typeof(Level), appenderInfo[2], true, out object enumParsed);
                    if (!isValidLevel)
                    {
                        writer.WriteLine(GlobalConstants.INVALID_INPUT);
                    }
                    else
                    {
                        appenderTreshold = (Level)enumParsed;
                    }
                }
                try
                {
                    ILayout   layout   = layoutFactory.CreateLayout(layoutType);
                    IAppender appender = appenderFactory.CreateAppender(appenderType, layout, appenderTreshold, file);
                    appenders.Add(appender);
                }
                catch (InvalidOperationException e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            ILogger logger = new Logger.Models.Logger(appenders);

            return(logger);
        }
Exemplo n.º 3
0
        public void Run()
        {
            List <IAppender> appenders = new List <IAppender>();

            int counter = int.Parse(Console.ReadLine());

            for (int i = 0; i < counter; i++)
            {
                string[] inputInfo = Console.ReadLine()
                                     .Split(" ", StringSplitOptions.RemoveEmptyEntries);


                string      appenderType = inputInfo[0];
                string      layoutType   = inputInfo[1];
                ReportLevel reportLevel  = ReportLevel.INFO;

                if (inputInfo.Length > 2)
                {
                    reportLevel = Enum.Parse <ReportLevel>(inputInfo[2], true);
                }

                ILayout   layout   = LayoutFactory.CreateLayout(layoutType);
                IAppender appender = AppenderFactory.CreateAppender(appenderType, layout, reportLevel);

                appenders.Add(appender);
            }

            string input = Console.ReadLine();

            ILoggers loggers = new Loggers(appenders.ToArray());

            while (input != "END")
            {
                string[] inputInfo = input.Split("|");

                string loggerType = inputInfo[0];
                string date       = inputInfo[1];
                string message    = inputInfo[2];

                if (loggerType == "INFO")
                {
                    loggers.Info(date, message);
                }
                if (loggerType == "ERROR")
                {
                    loggers.Error(date, message);
                }
                if (loggerType == "WARNING")
                {
                    loggers.Warning(date, message);
                }
                if (loggerType == "CRITICAL")
                {
                    loggers.Critical(date, message);
                }
                if (loggerType == "FATAL")
                {
                    loggers.Fatal(date, message);
                }

                input = Console.ReadLine();
            }

            Console.WriteLine("Logger info");

            foreach (var appender1 in appenders)
            {
                Console.WriteLine(appender1);
            }
        }