public async Task <int> DeleteProcessedLogFile(IisLogFile processedLogFile)
        {
            string connStr = _configuration.GetConnectionString(DatabaseName.IisDb);
            DbContextOptionsBuilder <IisLogDbContext> builder =
                new DbContextOptionsBuilder <IisLogDbContext>()
                .UseSqlServer(connStr);

            await using (IisLogDbContext ctx = new IisLogDbContext(builder.Options))
            {
                ctx.LogFiles.Remove(processedLogFile);
                return(ctx.SaveChanges());
            }
        }
Example #2
0
        private async Task ProcessLogFiles(SiteInfo iisSite, Stopwatch logFileStopwatch, string fileNameAndPath, IisSite site,
                                           Stopwatch taskStopwatch, DateTime processingDate)
        {
            logFileStopwatch.Start();
            var fileInfo = new FileInfo(fileNameAndPath);

            var processedLogFile = await _repo.GetProcessedLogFile(fileNameAndPath);

            if (processedLogFile?.DateImported < fileInfo.LastWriteTime && processedLogFile.Size != fileInfo.Length)
            {
                _logger.LogInformation($"Reprocessing log file:{fileNameAndPath}");
                await _repo.DeleteProcessedLogFile(processedLogFile);
            }

            if (site.LogFiles.Exists(f => f.LogFileAndPath.Matches(fileNameAndPath)))
            {
                _logger.LogInformation($"Skipping log file:{fileNameAndPath}");
                return;
            }

            _logger.LogInformation($"Importing StagedLogEntities.");
            List <StagedIisLogEntry> stagedLogEntries =
                _logFileProcessor.ImportLogFileIntoEntities(fileNameAndPath, iisSite.HostName).ToList();

            _logger.LogInformation($"Converting StagedLogEntities to LogEntries.");

            taskStopwatch.Start();
            List <IisLogEntry> logEntries = await Task.FromResult(IisLogEntry.FromEntities(stagedLogEntries));

            taskStopwatch.Stop();
            _logger.LogInformation(
                $"Elapse time Converting StagedLogEntities: {taskStopwatch.Elapsed.Seconds} seconds, {taskStopwatch.Elapsed.Milliseconds} milliseconds.");

            IisLogFile logFile = new IisLogFile(fileInfo, iisSite, stagedLogEntries,
                                                logEntries, site, processingDate, fileNameAndPath);

            _logger.LogInformation("Saving log results");

            await _repo.AddLogs(site, logFile, logEntries, stagedLogEntries);

            logFileStopwatch.Stop();

            _logger.LogInformation(
                $"Elapse time processing {fileNameAndPath}: {logFileStopwatch.Elapsed.Seconds} seconds, {logFileStopwatch.Elapsed.Milliseconds} milliseconds.");
        }
        public async Task <int> AddLogs(IisSite site, IisLogFile iisLogFile, IEnumerable <IisLogEntry> iisLogEntries, IEnumerable <StagedIisLogEntry> stagedIisLogEntries)
        {
            string connStr = _configuration.GetConnectionString(DatabaseName.IisDb);
            DbContextOptionsBuilder <IisLogDbContext> builder =
                new DbContextOptionsBuilder <IisLogDbContext>()
                .UseSqlServer(connStr);

            await using (IisLogDbContext ctx = new IisLogDbContext(builder.Options))
            {
                ctx.Attach(site);
                ctx.Entry(site).State = EntityState.Modified;
                site.LogFiles.Add(iisLogFile);
                iisLogFile.StagedLogEntries.AddRange(stagedIisLogEntries);
                site.LogEntries.AddRange(iisLogEntries);

                return(ctx.SaveChanges());
            }
        }