4

I have multiple resource files to support different languages. The user is presented with resources in his preferred language. Additional logging is being done to an application log, to which I would like to log using the neutral language (English) only.

I do not want a dedicated resource file for English only messages. The same messages shown to end users in their language should be able to be additionally logged in English to the application log file.

What would the best approach to doing this?

Elan
  • 6,084
  • 12
  • 64
  • 84
  • Why do you need configurable logs? Just hard code them. – Patrick Mar 16 '11 at 16:17
  • Please give an example of how you write to the log – Patrick Mar 16 '11 at 16:17
  • I prefer not to hard code messages into the application. As I already have resource strings defined in the resource file, I would like to use these and not hard code additional messages into the code. I use a custom logging class. Resource strings represent error messages and have parameter place holders {0}, {1}, etc., which are replaced at runtime to construct the final error message. I want to be able to log to the application log specifically in English. – Elan Mar 16 '11 at 17:34

1 Answers1

5

Try

string res = Resources.ResourceManager.GetString("...", CultureInfo.InvariantCulture);
Henrik
  • 23,186
  • 6
  • 42
  • 92
  • This would work, but we would lose the strong typing of accessing resource strings. This is error prone if there are typos in the resource name. – Elan Mar 16 '11 at 17:30
  • The Resource class has culture Property. You could set it to invariant before retrieving the value and revert it after retrieving the value. It's kind of a hack though. Alternative would to do your own code generation for your strings resource file. – Naraen Apr 01 '11 at 22:09
  • 2
    @Elan Fortunately we have `nameof` operator now so no more hardcoding resource names in the code.. `Resources.ResourceManager.GetString(nameof(Resources.MyResouceString),...)` – jirkamat Sep 08 '17 at 09:12
  • Yes, fortunately, we do have "nameof" now! :) – Elan Jan 28 '18 at 17:16
  • You only need `ResourceManager` when you want to load an External resource. Use `.Properties` instead. – Yousha Aleayoub Apr 13 '20 at 12:13