我们通过编写Exception来记录系统中发生的任何异常。消息发送到文件。但是,它们是在客户端的文化中编写的。土耳其语的错误对我来说意义不大。
那么,我们如何在不改变用户文化的情况下用英语记录错误消息呢?
我们通过编写Exception来记录系统中发生的任何异常。消息发送到文件。但是,它们是在客户端的文化中编写的。土耳其语的错误对我来说意义不大。
那么,我们如何在不改变用户文化的情况下用英语记录错误消息呢?
当前回答
英文异常消息
try
{
......
}
catch (Exception ex)
{
throw new UserFriendlyException(L("ExceptionmessagesinEnglish"));
}
然后进入本地化文件夹,把它放在projectName.xml中,然后添加
<text name="ExceptionmessagesinEnglish">Exception Message in English</text>
其他回答
CultureInfo oldCI = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture ("en-US");
Thread.CurrentThread.CurrentUICulture=new CultureInfo("en-US");
try
{
System.IO.StreamReader sr=new System.IO.StreamReader(@"c:\does-not-exist");
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
Thread.CurrentThread.CurrentCulture = oldCI;
Thread.CurrentThread.CurrentUICulture = oldCI;
没有解决方法。
Tks:)
使用扩展方法覆盖捕获块中的异常消息,检查抛出的消息是否来自代码,如下所述。
public static string GetEnglishMessageAndStackTrace(this Exception ex)
{
CultureInfo currentCulture = Thread.CurrentThread.CurrentUICulture;
try
{
dynamic exceptionInstanceLocal = System.Activator.CreateInstance(ex.GetType());
string str;
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
if (ex.Message == exceptionInstanceLocal.Message)
{
dynamic exceptionInstanceENG = System.Activator.CreateInstance(ex.GetType());
str = exceptionInstanceENG.ToString() + ex.StackTrace;
}
else
{
str = ex.ToString();
}
Thread.CurrentThread.CurrentUICulture = currentCulture;
return str;
}
catch (Exception)
{
Thread.CurrentThread.CurrentUICulture = currentCulture;
return ex.ToString();
}
这招对我很管用:
//Exception Class Extensions
public static class ExceptionExtensions
{
public static string EnMessage(this Exception ex)
{
CultureInfo oldCI = Thread.CurrentThread.CurrentCulture;
string englishExceptionMessage = ex.Message;
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
try
{
var objectType = Type.GetType(ex.GetType().FullName);
var instantiatedObject = Activator.CreateInstance(objectType);
throw (Exception)instantiatedObject;
}
catch (Exception e)
{
englishExceptionMessage = e.Message;
}
Thread.CurrentThread.CurrentCulture = oldCI;
Thread.CurrentThread.CurrentUICulture = oldCI;
return englishExceptionMessage;
}
}
然后你可以通过调用新方法ex.EnMessage()来使用它;
关于。net Core和以上,线程文档。CurrentUICulture推荐使用CultureInfo。属性检索和设置当前区域性。
CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;
在一个相关的GitHub问题上,Tarek Mahmoud Sayed建议使用CultureInfo。defaultthreadcurrentuicculture以确保稍后创建的其他线程也将获得相同的区域性:
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;
后者只在。net Framework 4.5(和。net Core 1.0)之后才可用。
这里有一个解决方案,不需要任何编码,甚至适用于异常的文本加载过早,我们能够通过代码更改(例如,那些在mscorlib)。
它可能并不总是适用于每一种情况(这取决于你的设置,因为你需要能够创建一个。config文件之外的主。exe文件),但这对我来说是可行的。因此,只需在dev中创建一个app.config,(或[myapp].exe。Config或web。Config in production),包含以下行,例如:
<configuration>
...
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="mscorlib.resources" publicKeyToken="b77a5c561934e089"
culture="fr" /> <!-- change this to your language -->
<bindingRedirect oldVersion="1.0.0.0-999.0.0.0" newVersion="999.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Xml.resources" publicKeyToken="b77a5c561934e089"
culture="fr" /> <!-- change this to your language -->
<bindingRedirect oldVersion="1.0.0.0-999.0.0.0" newVersion="999.0.0.0"/>
</dependentAssembly>
<!-- add other assemblies and other languages here -->
</assemblyBinding>
</runtime>
...
</configuration>
它的作用是告诉框架将mscorlib的资源和System.Xml的资源的程序集绑定重定向到一个程序集,用于版本介于1到999之间的法语(文化设置为“fr”)。不存在(任意版本999)。
因此,当CLR为这两个程序集(mscorlib和System.xml)寻找法语资源时,它不会找到它们,而是优雅地回退到英语。根据上下文和测试,您可能希望将其他程序集添加到这些重定向(包含本地化资源的程序集)。
当然,我认为微软不支持这个功能,所以请自行承担使用风险。好吧,如果您发现了问题,您可以删除这个配置并检查它是否无关。