/// <summary> /// Parse a log file. /// </summary> /// <param name="parser">The parser to use.</param> /// <param name="logFile">Path to a log file.</param> /// <returns>Task representing the parse operation.</returns> public static Task ParseAsync(this ILogParser parser, string logFile) { parseCounter.Increment(); var fr = new FileStream(logFile, FileMode.Open); parseFileSizeHistogram.Update(fr.Length, logFile); return(parser.ParseAsync(fr).ContinueWith(_ => fr.Dispose(), TaskContinuationOptions.ExecuteSynchronously)); }
public async Task Process() { var parseTask = logFileParser.ParseAsync(extraParserArguments[0]); System.Threading.Thread.Sleep(100); // Delay to prevent print from racing to the end of the logs before the parser can parse the first log var printTask = printer.PrintAsync(); await printTask; await parseTask; }
/// <summary> /// Parse a log file. /// </summary> /// <param name="parser">The parser to use.</param> /// <param name="logFile">Path to a log file.</param> /// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param> /// <returns>Task representing the parse operation.</returns> public static Task ParseAsync(this ILogParser parser, string logFile, CancellationToken cancellationToken) { parseCounter.Increment(); if (cancellationToken.IsCancellationRequested) { return(Task.FromCanceled(cancellationToken)); } var fr = new FileStream(logFile, FileMode.Open); parseFileSizeHistogram.Update(fr.Length, logFile); return(parser.ParseAsync(fr, cancellationToken).ContinueWith(_ => fr.Dispose(), TaskContinuationOptions.ExecuteSynchronously)); }
public async Task <IActionResult> Upload(UploadViewModel uploadModel) { var hosts = await repository.GetAllHostsAsync(); var hostsModel = mapper.Map <List <HostViewModel> >(hosts); if (!ModelState.IsValid) { return(View("Index", new HomeViewModel { Journal = new JournalViewModel { Hosts = mapper.Map <List <HostViewModel> >(hosts) }, UploadForm = uploadModel })); } var validExts = new string[] { ".txt", ".log" }; var filename = uploadModel.File.FileName; if (!validExts.Any(s => s.Equals(filename.Substring(filename.LastIndexOf("."))))) { ModelState.AddModelError(string.Empty, "Select valid file"); return(View("Index", new HomeViewModel { Journal = new JournalViewModel { Hosts = mapper.Map <List <HostViewModel> >(hosts) }, UploadForm = uploadModel })); } // full path to file in temp location var filePath = Path.GetTempFileName(); using (var stream = new FileStream(filePath, FileMode.Create)) { await uploadModel.File.CopyToAsync(stream); } var logEntries = await logParser.ParseAsync(filePath); var requests = mapper.Map <List <Request> >(logEntries); //remove present requests to avoid duplication var removedReqs = new List <Request>(); foreach (var req in requests) { if (repository.isRequestPresent(req.RequestorIPAddress, req.DateTimeRequested)) { removedReqs.Add(req); } } foreach (var req in removedReqs) { requests.Remove(req); } await repository.AddRequestsAsync(requests); await uow.CommitAsync(); hosts = await repository.GetAllHostsAsync(); hostsModel = mapper.Map <List <HostViewModel> >(hosts); var model = new HomeViewModel { Journal = new JournalViewModel { Hosts = hostsModel }, UploadForm = new UploadViewModel() }; return(View("Index", model)); }