private void PersistAccessLogFile(SqliteConnection connection, string filePath)
        {
            Execute(connection, "BEGIN");

            using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                // decompress, if necessary
                Stream readStream = fileStream;
                if (filePath.EndsWith(".gz"))
                {
                    readStream = new GZipStream(fileStream, CompressionMode.Decompress);
                }

                // parse the file, line by line
                using (var streamReader = new StreamReader(readStream, Encoding.UTF8))
                {
                    string line;
                    while ((line = streamReader.ReadLine()) != null)
                    {
                        var entry = _parser.ParseLine(line);
                        PersistEntry(connection, entry);
                    }
                }
            }

            Execute(connection, "COMMIT");
        }
Ejemplo n.º 2
0
        public IEnumerable <AccessLogEntry> GetLogEntryPages(ConnectionInfo connectionInfo, string remotePattern)
        {
            // get the list of files via SSH
            var remoteAccessLogList = GetRemoteAccessLogs(connectionInfo, remotePattern);

            using (var scpClient = new ScpClient(connectionInfo))
            {
                scpClient.Connect();

                foreach (var remotePath in remoteAccessLogList)
                {
                    var entries = new Stack <AccessLogEntry>();

                    // write the file to the temporary directory
                    string localPath = Path.GetTempFileName();
                    using (var localStream = new FileStream(localPath, FileMode.Create, FileAccess.ReadWrite))
                    {
                        // download the log file
                        scpClient.Download(remotePath, localStream);

                        // reset the file cursor to the beginning of the file
                        Stream readStream = localStream;
                        readStream.Position = 0;

                        // decompress the file, if necessary
                        if (remotePath.EndsWith(".gz"))
                        {
                            readStream = new GZipStream(localStream, CompressionMode.Decompress);
                        }

                        // read the file line by line
                        using (var reader = new StreamReader(readStream))
                        {
                            string line;
                            while ((line = reader.ReadLine()) != null)
                            {
                                entries.Push(_parser.ParseLine(line));
                            }
                        }
                    }

                    // remove the local file
                    File.Delete(localPath);

                    // yield the results
                    foreach (var entry in entries)
                    {
                        yield return(entry);
                    }
                }
            }
        }