9

I am logging error messages and .NET exceptions of my application in a database and display them on a GUI. The GUI displays error messages in the language of the locale of the user running the application. In order to achieve this I log resource names of error messages only, and turn them into meaningful messages using resource files.

I, however, cannot wrap my head around how to achieve the same for .NET standard exception messages. I can only log the message string, but then the language is already 'baked in' so I can only display the exception in that single language.

Is there a way to get the resource name of the .NET exception + all parameters that are inserted into the message string, so that I can log these?

2
  • Can you provide an example of how your using exception handling? Do you have a class that inherits from System.ApplicationException so all your errors catch MyAppException? Commented Jan 4, 2014 at 21:49
  • Are you using on the logging frameworks like Nlog or Log4Net? Commented Jan 4, 2014 at 21:52

2 Answers 2

2

There is no such thing as resource name for an exception, but there is the HResult property of the Exception class. The standard .NET framework exceptions have valid and unique HResult numbers which you can map then to your own resource identifier that you can translate then. Furthermore there is the Exception.Data property as well, which may contain additional information about the exception.

Or, you can parse the exception messages manually, using regex or such and then map to your resource id, but this is too much effort I think.

Sign up to request clarification or add additional context in comments.

Comments

0

I had this problem too and did a fair amount of research on it. Based on everything I could tell there was not a way to localize the exception itself because what you're seeing is not localized strings, it is actual code. The code is only written in one language (english) and can only be displayed as such.

3 Comments

that's not true. If you install the .Net language packages the exceptions are shown in the current language (for example if the application's culture is set to ES-ES the typical object reference not set to an instance of an object becomes referencia a objeto no establecida a una instancia de un objeto (in spanish)
I don't want to localize the exception (that's easy with language packs - as HighCore said - and 2 lines of code, btw). I want to get the resource name + parameters of the exception message string.
True, but in this case the exception is recorded in the culture of the application running at the time. Daniel wants to display the exception already recorded in a different language.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.