Recently I read Anna Karenina, and currently reading The Brothers Karamazov, and I find it odd that the upper classes in the books frequently speak French and there seems to be a fascination with France, but why?
The history of what seems like intense Russian fascination with France dates back to Peter the Great (1672-1725), his modernising (read: Europeanising) reforms, and innovations and affectations Peter would bring. Several years into his reign, Peter would undergo a mostly incognito tour of Western Europe, known as the Grand Embassy.
While Peter didn't visit France at that time, he did visit Britain and the Netherlands, and stayed in Europe for around 1.5 years. Peter, a relatively poorly-educated but keen auto-didact, even took the time to personally work in a Dutch shipyard, and to study English city-building techniques, which later inspired his new capital St Petersburg.
While the trip was of limited diplomatic success (Peter had been seeking alliances against the Ottoman Empire), Peter returned with renewed vigour to modernise Russia.
Around the turn of the century, Peter coerced and encouraged many seemingly cosmetic reforms, which included:
As Russia opened up the world, the language of diplomacy, French would now play a key role. To this day, many words in the French language, such as Palto (meaning overcoat, from the French 'paletot'), and Etazh, meaning floor or level.
Despite initial resistance to Peter's reforms, Russia's nobility largely seemed to take to French ways and customs - which were needed once Peter built St Petersburg - a very modern city compared to the traditional capital of Moscow.
For the wealthy and connected in Russia at the time, speaking French and following French customs was a way to distinguish yourself from poor, uneducated peasant masses.
Then, during the French revolution (1789-1799), many French former nobles in need of
safe haven would emigrate to Russia, some achieving high positions in government, while others became governors or staff for rich families. (This situation would be reversed somewhat in 1917, when many Russian nobles fled to France, Germany, the UK and the US).
Napoleon's invasion of Russia, and the previous Napoleonic war, would lead to something of a decline of this period of infatuation with all things French.
However, by-then old habits die hard, and when Russian troops entered Paris in 1814, a fascination with the modernity and culture of the city proved to be an unforgettable memory - perhaps in part influencing Russian army officers to carry out the unsuccessful Decembrist Revolt 11 years later in 1825.
Fast-forward half a century to the 1870s, and France is at the height of the Belle Epoque - its golden age - right at the the same time of Anna Karenina's serialised publication from 1875-1877. Thus, it may not be surprising to readers at the time that characters in the novel so revere all things French.
As u/kieslowskifan writes in his excellent answer, "The Entente and the still glowing embers of cultural prestige attached to France ensured that the French language remained an important aspect of Russian elite education long after the Enlightenment."
French had been a continental lingua franca for diplomacy and government for some time by the 1860s, the setting for Anna Karenina and Brothers Karamazov. War and Peace, for example, takes place between 1805 and 1812, and French plays an even greater role than in Anna Karenina, particularly with the friction of French as a prestige, aristocratic language given the backdrop.
The importance of Peter the Great has been outlined already, where French emerged as a diplomatic language; I would point to the reign of Catherine the Great (1762 - 1796) for the preeminent position of French as an aristocratic language. Catherine is most famous as an enlightened despot and woman of letters, who corresponded with Voltaire and Diderot, among other philosophes. Catherine established French as the court language, and the length of her reign solidified French as a significant feature of Russian aristocratic life; the importation of French intellectuals throughout her reign and revolutionary emigres in the following decade only solidified French's position (though German remained important diplomatically, it was characterized as somewhat demode).
The (over)influence of French became a point of contention during the debates on the character of Russia in the 1840s, and again during the reign of Alexander, when these novels are set: Slavophiles found the estrangement from their native language and imagined heritage horrible, while so-called Westernizers argued that if Russia should remain a Great Power it must adopt the trappings of the cultures it kept at arm's length. This intellectual debate courses through both Dostoyevsky and Tolstoi: the former a Slavophile, who satirized and derided Russians who hardly knew the language, the latter an ascetic anarchist.
Part of the answer to this would be similar to the answer to the question posed in the future about our time, why in the upper classes most people speak English fluently. After the Enlightenment, French supplanted Latin as the language of international communication. This phenomenon in Russia is also called Gallomania.
Specifically in Russia under Peter the French did not manage to gain a foothold as the Dutch or the Danes did.
The popularity of the French language increased under the empress Elizaveta Petrovna, in 1846, Voltaire at the opening speech in the academy of Sciences thanked the Empress for her patronage of French speech and tastes. The growth of French influence seems to have begun even stronger in 1756, with the outbreak of the Seven Years' War and the alliance with France. Under Elizabeth, French furniture and French statues, French costumes, French cuisine begin to predominate in the interiors of the nobility. Molière's plays begin to be staged in theaters, and in architecture the French begin to compete with the Italians. Also children begin to be taught by the French model (under Peter the nobility had to send children to study for captains and military engineers in Holland or London, under Peter, in Russia came to work some French scientists: astronomer Delillet or mathematician Bernoulli, but they could not find students simply because almost no one spoke French. )
The interest in France persisted under Catherine the Great, who was brought up on Encyclopedist literature, Diderot came to visit her and stayed with her for five months. She bought his library from him, in fact it was a form of patronage at the time. She bought his library and appointed him librarian there, i.e., he received a salary for the possession of his library, which he undertook to give to her after his death.
The next major growth of French culture, as is not difficult to guess, was provided by the French Revolution, which gave a large number of refugees. Some decline in interest comes after the war with Napoleon.
As a traditionalist, Tolstoy was rather opposed to the dominance of the French language. If you have noticed, in the first volume of War and Peace the aristocracy speaks French to one another almost uniformly, a sign of the times, but for Tolstoy it is also a symbol of the detachment of the aristocracy from the people. He thought it was necessary to go back to the roots, so to speak.