コード例 #1
0
        public static void Main()
        {
            IReader reader = new Reader();
            IWriter writer = new Writer();

            LayoutFactory   layoutFactory   = new LayoutFactory();
            AppenderFactory appenderFactory = new AppenderFactory();

            Controller controller = new Controller(layoutFactory, appenderFactory);
            Engine     engine     = new Engine(reader, writer, controller);

            engine.Run();
        }
コード例 #2
0
        public static void Main()
        {
            var appendersCount = int.Parse(Console.ReadLine());

            var appenders       = new List <IAppender>();
            var layoutFactory   = new LayoutFactory();
            var logFileFactory  = new LogFileFactory();
            var appenderFactory = new AppenderFactory(layoutFactory, logFileFactory);

            ReadAppendersData(appendersCount, appenders, appenderFactory);

            var logger = new Logger(appenders);

            var errorFactory = new ErrorFactory();

            var engine = new Engine(logger, errorFactory);

            engine.Run();
        }
コード例 #3
0
        private static void RunLastPart()
        {
            var n         = int.Parse(Console.ReadLine());
            var appenders = new List <IAppender>();

            for (int i = 0; i < n; i++)
            {
                var input    = Console.ReadLine().Split();
                var layout   = LayoutFactory.GetInstance(input[1]);
                var appender = AppenderFactory.GetInstance(input[0], layout);
                if (input.Length > 2)
                {
                    var reportLevel = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(input[2].ToLower());
                    appender.ReportLevel = (ReportLevel)Enum.Parse(typeof(ReportLevel), reportLevel);
                }
                appenders.Add(appender);
            }

            var    logger = new Loger(appenders);
            string command;

            while ((command = Console.ReadLine()) != "END")
            {
                if (command != null)
                {
                    var tokens = command.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

                    var loggerMethod = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(tokens[0].ToLower());
                    var methodType   = typeof(Loger).GetMethod(loggerMethod);
                    methodType.Invoke(logger, new object[] { tokens[1], tokens[2] });
                }
            }

            Console.WriteLine(@"Logger info");
            foreach (IAppender appender in appenders)
            {
                Console.WriteLine($"Appender type: {appender.GetType().Name}, Layout type: {appender.Layout.GetType().Name}, Report level: {appender.ReportLevel.ToString().ToUpper()}, Messages appended: {appender.File.MessageNumber}, File size {appender.File.Size}");
            }
        }
コード例 #4
0
        public static void Main()
        {
            //ILayout simpleLayout = new SimpleLayout();
            //IAppender consoleAppender = new ConsoleAppender(simpleLayout);
            //ILogger logger = new Logger(consoleAppender);

            //logger.Error("3/26/2015 2:08:11 PM", "Error parsing JSON.");
            //logger.Info("3/26/2015 2:08:11 PM", "User Pesho successfully registered.");

            //var simpleLayout = new SimpleLayout();
            //var xmlLayout = new XmlLayout();
            //var consoleAppender = new ConsoleAppender(xmlLayout);

            //var file = new LogFile();
            //var fileAppender = new FileAppender(xmlLayout, file);

            //var logger = new Logger(consoleAppender, fileAppender);
            //logger.Error("3/26/2015 2:08:11 PM", "Error parsing JSON.");
            //logger.Info("3/26/2015 2:08:11 PM", "User Pesho successfully registered.");

            //var simpleLayout = new SimpleLayout();
            //var consoleAppender = new ConsoleAppender(simpleLayout);
            //consoleAppender.ReportLevel = ReportLevel.Error;

            //var file = new LogFile();
            //var fileAppender = new FileAppender(xmlLayout, file);
            //fileAppender.ReportLevel = ReportLevel.Fatal;

            //var logger = new Logger(consoleAppender, fileAppender);

            //logger.Info("3/31/2015 5:33:07 PM", "Everything seems fine");
            //logger.Warning("3/31/2015 5:33:07 PM", "Warning: ping is too high - disconnect imminent");
            //logger.Error("3/31/2015 5:33:07 PM", "Error parsing request");
            //logger.Critical("3/31/2015 5:33:07 PM", "No connection string found in App.config");
            //logger.Fatal("3/31/2015 5:33:07 PM", "mscorlib.dll does not respond");

            var     layoutFactory   = new LayoutFactory();
            var     appenderFactory = new AppenderFactory();
            IReader reader          = new ConsoleReader();

            IAppender[] appenders = ReadAllAppenders(reader, layoutFactory, appenderFactory);
            var         logger    = new Logger(appenders);

            var commandInterpreter = new CommandInterpreter(logger);
            var cmdFactory         = new CommandFactory();

            List <string[]> msgsArgs = ReadAllMessages(reader);

            foreach (var msgArgs in msgsArgs)
            {
                string reportLevel = msgArgs[0];
                string dateTime    = msgArgs[1];
                string msg         = msgArgs[2];

                ReportLevel level;
                if (!Enum.TryParse(reportLevel, true, out level))
                {
                    throw new ArgumentException("Invalid level!");
                }

                Command command = cmdFactory.GetCommand(level, dateTime, msg);
                commandInterpreter.Execute(command);
            }

            Console.WriteLine(logger);
        }
コード例 #5
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);
        }
コード例 #6
0
 public Controller()
 {
     this.appenderFactory        = new AppenderFactory(AppendersNamespace);
     this.layoutFactory          = new LayoutFactory(LayoutsNamespece);
     this.writableObjectsFactory = new WritableObjectFactory(WritableObjectsNamespace);
 }
コード例 #7
0
ファイル: Engine.cs プロジェクト: BlueButterflies/CsharpOOP
        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);
            }
        }