There are a couple of Gemarot we need to know, the interpretation of which leads to two main opinions in commentators and poskim.
See Masechet Shabbat 64b, 146b, Betsa 9a, AZ 12b.:
Rab Yehuda said in Rav's name: Wherever the Sages forbade [aught] for appearances' sake, it is forbidden even In one's innermost chambers.
The Gemara Shabbat 64b - 65a, explained that regarding this Psak of rav is a already discussed by Tanayim. Indeed, the Mishna (chapter 22, folio 146b in Gemara) says:
If one's garments fall into water on the road, he may walk in them without fear when he reaches the outermost courtyard he may spread them out in the sun, but not in sight of the people.
This Mishna is against Rav because she makes a difference between in sight of the people and the courtyard. The Gemara reports a Berayta which begins with the statement of the Mishna and ends by the opinion of Rabbi El'azar and Rabbi Shim'on in Berayta which is the opinion of Rav above cited.
On the one hand, it seems that the Gemara ruled as Rav, against the Mishna, which is a minority opinion. This will mean that _If his garments falls into water on the road, he may walk in them without fear, but when he reaches the outermost courtyard he may NOT spread them out in the sun. Indeed, the Rambam and the Shulchan Aruch did not follow the mishna, but follow Rav, Rabbi El'azar and Rabbi Shim'on. Even in the courtyard, one may not spread the garments because of Mar'it Ayin, it seems as he did wash them on Shabbat.
But in other places, the Gemara did not stipulate that there is no difference between public or private situation. For instance a similar law is in Chulin, the Mishna in Chulin (41a) says that:
one may not slaughter at all into a pit; yet a person may dig a pit in his own house for the blood to run into. in the street, however, he should not do so lest he appear to follow the ways of the heretics.
The Gemara seems to accept this mishna without mentioning the rule of Rav (which appears to contradict the Mishna). Tosfot in Chulin 41a concludes, with a proof from the Yerushalmi (Kilayim 9, halacha 1) that Rav is rejected (the Yerushalmi says that this Mishna also is against Rav, and here the Bavli accepts it despite this fact). I.e. in Shabbat 64b - 65a, the Gemara rejected the psak of Rav and extablished that Rav was following the opinion of RE and RS in Berayita, which was rejected by Rabbi in the last conclusion of the Mishna. We say in Talmudic jargon that "Rav was held in Shita". We can say also that the suggiot of Chulin and Shabbat 64b are not congruent. So, Tosfot, (following Rav Nisim Gaon according to the text of Tosfot in Avoda Zara 12a) rejected the Psak of Rav and ruled the two stam Mishnayot (Shabbat 146b and Chulin 41a) according to the Suggia of Chulin.
The same Tosfot in Chulin 41a, in a first explanation (which is congruent with the opionion of Rambam and SA) says that the Mishna of Chulin addresses a different problem of Mar'it Haayn (he thinks that the Bavli in Chulin disagrees with the Yerushalmi). The Bavli thinks, according to Tosfot, that the symbolic meaning of the same action of blood evacuation is different in his own house than in public place: At home it is obvious that he wants to keep clean his yard. on the street it is very plausible to see him as following the ways of heretics. So this Mishna is consistent with Rav. The Shulchan Aruch, following Tur and Rambam ruled the mishna in Chulin despite that they ruled as Rav regarding Mar'it Ayn (against the Yerushalmi's logic).
In an area where there are no Jews --> YES
Tosfot Chulin 41a added in the end of the paragraph that even according to Rav, if the prohibition is not a scriptural sin, it's permitted to make the ambiguous action in a private way. He is followed in this by the Bet Yosef OC 336.
Are there exceptions --> YES for non-scriptural issues. There is one case of non scriptural sin in Tosfot: To remove blockage from a pipe in the roof (it is derabanan, mel'acha killeachar yad, this case is not addressed in Gemara for the issue of Mar'it Haayn); an additional case is to take a goat to walking on the street with a small bell, even if the bell cannot tinkling because of the Mar'it Haayn as if he is going to sell it in the market. To sell is a prohibition derabanan. The poskim ask a kushia against this principle of Tosfot. The Gemara in Shabbat 64b asks a contradiction between Rav and this din (of bell). There are many terutsim. (The Magen Avraham's teruts (as I understand it) is that the suggiot are the Suggia of the Bavli in Chulin is the halacha, and makes the case of Schita with the blood and the bell as case that are problematic on the street only. The svara is very understandable, a goat with a bell at home or in the yard is far from the market idea).
Is it a blanket rule--> For the Shulchan Aruch YES.
What is the logic? --> The understanding of the rule of Mar'it Haayin following Rav may be that a man needs to be aware of a kind of "mirror effect" , the reflect of his own actions can have a bad influence for himself. He have know that an ambiguous behaviour is bad.
But it may be noted that following the both opinions of Rambam and Rav Nisim Gaon, there is no difference between two public situations, the one in presence of Jewish, the other in presence of non Jewish people only, if people identify you as Jewish or not (for instance there are countries in which people don't know to identify a Jew).
Does it depend on being recognized as a Jew?
If you are not recognized as Jew, following to Rambam the fact that you recognize yourself is sufficient to prohibit.
For the RNG and his followers, concerning eventually prohibition as (Shabbat) which regard Jews only, I think that it is also prohibited because of the public character of the place, one cannot hide himself. public situation implies Mar'it Haayn anyway. (not sure 100‰)
The Yerushalmi needs to be quoted, he enumerates six mishnayot or beraytot which he classifies as not conguent to Rav:
אבל אסורין מפני מראית העין (כלאים ט, ב) : רב אמר כל שהוא אסור מפני מראית העין אפילו בחדרי חדרים אסור.
מתניתה פליגא על רב פשתן שצבעה <בחרס> [בחרת] לא יעשה ממנה אימרה מפורסמה. בכרים ובכסתות מותר.
מתניתא פליגא על רב נתפזרו מעותיו לפני עבודה זרה לא יהא שוחח ומלקטן שלא יהא כמשתחוה לעבודה זרה ואם היה מקום צנוע מותר.
מתניתא פליגא על רב פרצופות שהן מטילין מים בכרכים לא יתן פיו על פיו שלא יהא כמנשק לע"ז ואם היה מקום צנוע מותר. מתניתא פליגא על רב אין שוחטין בגומא אבל עושה הוא גומא
The Mishna cited in Chulin (41a):
- בתוך ביתו בשביל שיכנס הדם בתוכו ובשוק לא יעשה כן שלא יחקה את המינין.
The Mishna in Shabbat (146b):
מתניתא פליגא על רב הגיע לחצר החיצונה שוטחן בחמה אבל לא כנגד העם.
מתניתא פליגא על רב ביב שהוא קמור ארבע אמות ברשות הרבים אין שופכין לתוכו מים בשבת. ותני עלה אם היה מזחילה מותר. עונת גשמים מותר. צינורות מקלחין אסורין. תני בר קפרא אם היה מקום צנוע מותר.
The conclusion is that all this mishnayot and beraytot are against the words of Rav and there is no halachic relevance to the words of Rav (according to the Pene Moshe paraphrase, but according to the paraphrase of the Gilayon Hashass vetsiun Yerusthalaym, the irrelevance is decreased for the beraytot and Mishnayot that are against Rav).
- אילין פליגין על רב. ולית להון קיום:
In conclusion, the Yerushalmi as we can read it in our books is not clearly a support for Rav Nisim Gaon regarding his conclusion but it is an excellent source for the comparison between beraytot and the rule of Rav. The psak of Rav Nisim Gaon is mainly based on the the multiple places in which those beraytot and mishnayot are reported without report of the rule of Rav.