Example #1
0
        public static void serializeconfig()
        {
            try
            {

                Logfiles logfiles = new Logfiles();
                logfiles.ListFiles = new List<Logfile>();

                Logfile logfile = new Logfile();
                logfile.listpatterns.Add(new Pattern() { ptype = PType.exclude, patterntext = "text to exclude" });
                logfile.listpatterns.Add(new Pattern() { ptype = PType.exclude, patterntext = "text2 to exclude" });
                logfile.listpatterns.Add(new Pattern() { ptype = PType.include, patterntext = "text to include" });

                logfiles.ListFiles.Add(new Logfile { filename = "file1.txt", active = true, checkupdatetime = true, updatetimelimit = 20, listpatterns = logfile.listpatterns });
                logfiles.ListFiles.Add(new Logfile { filename = "file2.txt", active = true, checkupdatetime = false });

                XmlSerializer xmlSerializer = new XmlSerializer(typeof(Logfiles));
                StreamWriter sw = new StreamWriter("logfileconfig.xml");
                xmlSerializer.Serialize(sw, logfiles);
                sw.Close();
                Console.WriteLine("Serialize complete");

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

            }
        }
Example #2
0
        //return number of lines
        public static int Checkfile(Logfile log)
        {
            if (log.active == true)

            {
                Console.WriteLine("Start check {0} from position {1}", log.filename, log.lastreadposition);
                #region check file exists
                if (!System.IO.File.Exists(log.filename))
                {
                    Console.WriteLine("Log file not found:{0}", log.filename);

                    return 0;
                }

                else
                #endregion
                {
                    try
                    {
                        int i = -1;
                        int numerror = 0;
                        FileStream file = File.Open(log.filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                        StreamReader sr = new StreamReader(file, Encoding.GetEncoding("windows-1251"));

                         while (!sr.EndOfStream)
                           {

                            string line = sr.ReadLine();

                            i += 1;
                            if (i > log.lastreadposition)
                                {

                                #region check patterns and output
                                //string line = sr.ReadLine();

                                List<string> incpatterns = new List<string>();
                                List<string> excpatterns = new List<string>();

                                foreach (Pattern p in log.listpatterns)
                                {
                                    if (p.ptype == PType.include)
                                    {
                                        incpatterns.Add(p.patterntext);
                                    }

                                    if (p.ptype == PType.exclude)
                                    {
                                        excpatterns.Add(p.patterntext);
                                    }
                                }
                                if (incpatterns.Any(line.Contains))
                                {

                                    if (!excpatterns.Any(line.Contains))
                                    {

                                        numerror += 1;
                                        Console.WriteLine(line);
                                        Console.WriteLine(GetTime(line));

                                    }

                                }
                                #endregion

                                }
                            //Console.WriteLine("Line time is {0}",GetTime(line));
                            }
                            Console.WriteLine("Total lines: {0}", i);
                            Console.WriteLine("Total number of errors: {0}", numerror);

                        return i;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        return 0;

                    }

                }

             }

            else
            {
                Console.WriteLine("Skip logfile {0}", log.filename);
                return 0;

            }
        }