Ejemplo n.º 1
0
        /// <summary>
        /// Read a specific file, and insert the new lines to the database.
        ///
        /// </summary>
        /// <param name="fullPath"></param>
        static void writeOutChanges(string fullPath)
        {
            if (stateOfFiles.ContainsKey(fullPath))
            {
                using (var fs = new FileStream(fullPath, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite))
                    using (var sr = new StreamReader(fs))
                    {
                        string line;
                        long   offset = 0;
                        //read the first part of the file (no changes here)
                        for (long i = 0; i < stateOfFiles[fullPath]; i++)
                        {
                            sr.ReadLine();
                        }

                        PostgreSQL pg = new PostgreSQL(dbconn); //open the DB connection
                        //read the new lines which appended to the file
                        while ((line = sr.ReadLine()) != null)
                        {
                            offset++;
                            try {
                                pg.insertToServerlogsTable(Path.GetFileName(fullPath), line);
                                Console.WriteLine(line);
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine("Cannot add to database this line: " + line + "\nException: " + e.StackTrace);
                            }
                        }
                        pg.closeDB(); //close database connection
                        stateOfFiles[fullPath] += offset;
                    }
            }
            else
            {
                using (var fs = new FileStream(fullPath, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite))
                    using (var sr = new StreamReader(fs))
                    {
                        long lineCounter = 0;
                        while (sr.ReadLine() != null)
                        {
                            lineCounter++;
                        }
                        stateOfFiles.Add(fullPath, lineCounter);
                    }
            }
        }
        /// <summary>
        /// Read a specific file, and insert the new lines to the database. 
        /// 
        /// </summary>
        /// <param name="fullPath"></param>
        static void writeOutChanges(string fullPath)
        {
            if (stateOfFiles.ContainsKey(fullPath))
            {
                using (var fs = new FileStream(fullPath, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite))
                using (var sr = new StreamReader(fs))
                {
                    string line;
                    long offset = 0;
                    //read the first part of the file (no changes here)
                    for (long i = 0; i < stateOfFiles[fullPath]; i++)
                        sr.ReadLine();

                    PostgreSQL pg = new PostgreSQL(dbconn); //open the DB connection
                    //read the new lines which appended to the file
                    while ((line = sr.ReadLine()) != null)
                    {
                        offset++;
                        try {
                            pg.insertToServerlogsTable(Path.GetFileName(fullPath), line);
                            Console.WriteLine(line);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("Cannot add to database this line: "+line +"\nException: "+ e.StackTrace);
                        }
                    }
                    pg.closeDB(); //close database connection
                    stateOfFiles[fullPath] += offset;
                }
            }
            else
            {
                using (var fs = new FileStream(fullPath, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite))
                using (var sr = new StreamReader(fs))
                {
                    long lineCounter = 0;
                    while (sr.ReadLine() != null)
                        lineCounter++;
                    stateOfFiles.Add(fullPath, lineCounter);
                }
            }
        }