简体   繁体   中英

Log and display .NET exceptions in multiple languages

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM