/// <summary> /// Process errors. Log error details and copy the file to exception folder. /// </summary> /// <param name="filePath">Original file path</param> /// <param name="fileName">File name</param> /// <param name="result">The result from reader</param> public void ProcessingErrors(string filePath, string fileName, EventReaderResult result) { var exceptionFileFolder = _configuration["exceptionFolder"]; foreach (var error in result.Errors) { _logger.LogInfoToConsole($"{filePath} meet following errors: {error}"); } if (!Directory.Exists(exceptionFileFolder)) { throw new Exception($"{exceptionFileFolder} does not exist."); } var exceptionFilePath = Path.Combine(exceptionFileFolder, fileName); if (File.Exists(exceptionFilePath)) { throw new Exception($"{exceptionFilePath} has existed."); } File.Copy(filePath, exceptionFilePath); }
/// <summary> /// Create new json file, using validator to validate each event. Based on /// the result of validation to create new json file in different folder or /// log error message to console. Returning processed eventDetails object. /// </summary> /// <param name="eventDetails">Used to fill in event details</param> /// <param name="result">Used to get each event</param> /// <returns>Returns EventDetails object</returns> public EventDetails ProcessingEvent(EventDetails eventDetails, EventReaderResult result) { foreach (var flightEvent in result.Events) { string timeStamp = DateTime.Now.ToString("yyyyMMddHHmmssFFF"); string flightEventTypeName = flightEvent.EventType.ToLower(); //GetSection Never return null, If no matching sub-section is found with the specified key, an empty IConfigurationSection will be returned. var configSection = _configuration.GetSection(flightEventTypeName).GetChildren(); var configSectionList = configSection.ToList(); //Check configSection is empty or not if (configSectionList.Any()) { string curatedFolder = null; string exceptionFolder = null; foreach (var config in configSection) { if (config.Key == FolderType.Curated.ToString().ToLower()) { curatedFolder = config.Value; } if (config.Key == FolderType.Exception.ToString().ToLower()) { exceptionFolder = config.Value; } } // Count of events per type. if (!eventDetails.EventDetailsList.TryAdd(flightEventTypeName, 1)) { var count = ++eventDetails.EventDetailsList[flightEventTypeName]; eventDetails.EventDetailsList[flightEventTypeName] = count; } var arrivalEventJson = JsonConvert.SerializeObject(flightEvent); // Validate each event if (_validator.IsValidate(flightEvent)) { if (!String.IsNullOrEmpty(curatedFolder)) { CreateFileHelper(arrivalEventJson, curatedFolder, timeStamp, flightEventTypeName); } else { _logger.LogInfoToConsole($"{flightEventTypeName} has no curated folder setup in appsettings.json."); } } else { if (!String.IsNullOrEmpty(exceptionFolder)) { CreateFileHelper(arrivalEventJson, exceptionFolder, timeStamp, flightEventTypeName); eventDetails.FailedEventList.Add($"{flightEventTypeName}-{timeStamp}"); eventDetails.FailedEventCount++; } else { _logger.LogInfoToConsole($"{flightEventTypeName} has no exception folder setup in appsettings.json."); } } } else { _logger.LogInfoToConsole($"{flightEventTypeName} has no folder path setup in appsettings.json"); } } return(eventDetails); }