public async Task <long?> AddTradeAsync(TradePost trade) { var config = new CsvConfiguration(CultureInfo.InvariantCulture); var fileExist = File.Exists(_configuration.Path); var addHeader = !fileExist || (File.ReadLines(_configuration.Path).FirstOrDefault()?.Length ?? 0) == 0; await using var stream = File.Open(_configuration.Path, FileMode.Append); await using var writer = new StreamWriter(stream); await using var csv = new CsvWriter(writer, config); if (addHeader) { csv.WriteHeader <CsvTrade>(); await csv.NextRecordAsync(); } csv.WriteRecord(new CsvTrade() { TransactionType = trade.TransactionType ?? "", Price = trade.Price, MarketCode = trade.Market ?? "", TradeDate = trade.TransactionDate.DateTime, Quantity = trade.Quantity, Comments = trade.Comments, BrokerageCurrencyCode = trade.BrokerageCurrencyCode, InstrumentCode = trade.Symbol ?? "", PortfolioId = trade.PortfolioId?.ToString() ?? "" }); await csv.NextRecordAsync(); return(trade.GetHashCode()); }