Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
139 views
in Technique[技术] by (71.8m points)

c# - How to turn on CircuitOptions.DetailedErrors?

I'm getting this message in the console when running a server-side Blazor app:

Error: There was an unhandled exception on the current circuit, so this circuit will be terminated. For more details turn on detailed exceptions in 'CircuitOptions.DetailedErrors'

I've had a look at the Blazor error handling documentation, but I can't work out how to actually turn on the detailed errors mentioned in that message?

question from:https://stackoverflow.com/questions/57514541/how-to-turn-on-circuitoptions-detailederrors

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

More digging on this revealed that there are both non-Blazor specific .NET Core ways to turn on Detailed Errors, and also a Blazor specific approach:

The general .NET Core way to turn on Detailed Errors:

There are a number of ways to get the detailed errors as discussed in the .NET Core documentation, but I ended up using the Detailed Errors setting:

WebHost.CreateDefaultBuilder(args).UseSetting(WebHostDefaults.DetailedErrorsKey, "true")

And the Development Environment setting:

WebHost.CreateDefaultBuilder(args).UseEnvironment(Environments.Development)

Both of those are used in Program.cs:

If you are using the older (and eventually to be deprecated IWebHostBuilder approach) that looks like this:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseSetting(WebHostDefaults.DetailedErrorsKey, "true")
        //.UseEnvironment(EnvironmentName.Development)
        .UseStartup<Startup>();

And if you're using the newer IHostBuilder approach that was introduced with Core 2.1 that looks like this:

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder
                .UseStartup<Startup>()
                .UseSetting(WebHostDefaults.DetailedErrorsKey, "true")
                //.UseEnvironment(EnvironmentName.Development);
        });

Once I set that I got more details about my misfiring Blazor code.

A Blazor specific approach:

An alternative approach for turning on detailed errors can also be found in this answer, which includes this code:

services.AddServerSideBlazor().AddCircuitOptions(options => {  options.DetailedErrors = true; });

This approach can then be expanded to include a check for whether the code is being run in the development environment

services.AddServerSideBlazor().AddCircuitOptions(o =>
{
    if (_env.IsDevelopment()) //only add details when debugging
    {
        o.DetailedErrors = true;
    }
});

as highlighted by @Eonasdan's answer below


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...