应用程序未处理的异常本地化 [英] Application unhandled exceptions localization

查看:112
本文介绍了应用程序未处理的异常本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以语言环境语言显示错误消息,并且对于所有已处理的异常,我的团队正在使用资源文件以本地语言显示,但是
是否可以拦截中间件以显示应用程序未处理语言环境语言中的异常?

I am trying to display error messages in locale language and for all handled exceptions my team is using resource file to display in local language but Is there a way to intercept the middleware to display application unhandled exceptions in locale language?

推荐答案

标准.NET异常已本地化,消息语言将取决于当前的线程文化。因此,要使其发挥作用,您将需要实现RequestCultureMiddleware,该软件将根据您的需求更改语言。下面是一个示例:

Standard .NET exceptions are localized and message language will depend on current thread culture. So for you to make it work you'll need to implement RequestCultureMiddleware that would change the language based on your needs. Here is an example:

public class RequestCultureMiddleware
{
    private readonly RequestDelegate next;

    public RequestCultureMiddleware(RequestDelegate next)
    {
        this.next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        // Get it from HTTP context as needed
        var language = "fr-FR";

        var culture = new System.Globalization.CultureInfo(language);
        System.Threading.Thread.CurrentThread.CurrentCulture = culture;
        await next(context);
    }
}

在MVC之前注册 Startup 类中:

app.UseMiddleware(typeof(RequestCultureMiddleware));
app.UseMvc();

请注意:此处不显示异常消息。

Please note: displaying exception message is not covered here.

这篇关于应用程序未处理的异常本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆