Format exception messages using InvariantCulture or CurrentCulture?

When throwing exceptions, I often pass in a formatted string that provides detailed information about the problem that has occurred. I always indicate the formatting provider if possible (which is good practice, because otherwise you may forget to decide which culture is suitable, and the current culture by default, which can lead to numerous errors).

Here is an example:

throw new InvalidOperationException(
    string.Format(
        CultureInfo.CurrentCulture,
        "{0} is a bad number.",
        number));

I am tempted to use CurrentCulture, as shown above, because exception messages are aimed at people (of course, the code should never act on the exception message itself). The message will be formatted using the client’s culture, so whenever I need to display it on my client, it looks good.

However, in addition to displaying messages to users, exceptions can also be logged in the log file. I saw that many messages are sitting in my log files, with all types of cultures used to format them. Pretty ugly! In this case, it InvariantCulturewill be more appropriate or, possibly, the culture of the server on which the log file is located.

, , , . , , , .NET.

, ?

+3
2

, . ?

, , , (,.NET framework), . , , (.. ).

. , . , , ( , ).

( -) , . , () , ( , , ) .

WCF, , , . . IncludeExceptionDetailInFaults .

+5

IMHO Data . , "" , . "" Key = "UIMessage" Value = uf, ( ). Message , .

+1

Source: https://habr.com/ru/post/1753515/


All Articles