59

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?

tomRedox
  • 28,092
  • 24
  • 117
  • 154

5 Answers5

74

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 modern approach

The methods I detail below date from the .NET 2.1 era and things have improved a lot since then. Tyson Gibby's answer below is a better way to handle this in general now, so I've changed that to the accepted answer.

I've left the two approaches below for historic reference anyone needing answers for earlier versions of Blazor.

[Outdated] 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.

[Outdated] 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 =>
{
    //only add details when debugging
    o.DetailedErrors = _env.IsDevelopment();
});

as highlighted by @Eonasdan's answer below

tomRedox
  • 28,092
  • 24
  • 117
  • 154
  • 1
    EnvironmentName.Development – Simon Foster Aug 29 '19 at 16:23
  • oops I posted too soon, was going to say VS recommends using Environments.Development rather than EnvironmentName.Development which is marked as obsolete – Simon Foster Aug 29 '19 at 22:22
  • @SimonFoster thank you, I hadn't spotted that. I've updated the answer to use that. – tomRedox Aug 31 '19 at 12:33
  • 1
    This is super useful. Another approach could also be to just #if DEBUG the additional line so that the enhanced errors are disabled in a release build. – Webreaper Oct 15 '19 at 07:03
  • 1
    How to obtain _env (a IWebHostEnvironment) in the ConfigureServices method? – hybrid2102 Oct 16 '19 at 08:09
  • I ended up using ConfigureDevelopmentServices method: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-3.0 – hybrid2102 Oct 16 '19 at 08:16
  • `IHostBuilder` doesn't have a `.UseSetting` method. – BrainSlugs83 Nov 07 '19 at 21:04
  • @BrainSlugs83, good point, I've added an example that uses the new `IHostBuilder` syntax. That example's from our live project, so it should be working. – tomRedox Nov 08 '19 at 08:25
  • This worked perfectly for me services.AddServerSideBlazor().AddCircuitOptions(options => { options.DetailedErrors = true; }); – Greg Trevellick Jul 05 '20 at 19:49
  • Is there a way to do this once the code is compiled? I can't find Program.cs. – LuVu Feb 10 '21 at 15:21
  • @LuVu not to my knowledge. What type of project are you working with (MVC, Blazor, or something else?) – tomRedox Feb 10 '21 at 17:46
  • It is a C# .NET project using Blazor pages. I deployed it on IIS server. In my development environment my application is working fine, but in production I get the following message in the console: Error: There was an unhandled exception on the current circuit, so this circuit will be terminated. For more details turn on detailed exceptions by setting 'DetailedErrors: true' in 'appSettings.Development.json' or set 'CircuitOptions.DetailedErrors'. – LuVu Feb 10 '21 at 18:50
  • @LuVu there should definitely be a Program.cs file in the root of your web UI project - it contains the Main method which runs the app. Try a global search for "public class Program" – tomRedox Feb 10 '21 at 18:55
  • I would definitely recommend Tyson Gibby's answer using appsettings.Development.json. It handles detection of environments, those configuration files are perfect for this kind of setting. – Gunnar Már Óttarsson Feb 18 '22 at 19:02
31

NO CODE : EASIER & More SECURE

Best Practice

This is easier than most of the proposed solutions and it does not introduce a possible security issue into the code. It is also considered a coding best practice.

Microsoft recommends adding the following to the appsettings.development.json file as it does not add code to the application that can become a security risk. It is not recommended to put this in appsettings.json as this settings file is reserved for the production environment.

You can also use this approach to provide detailed error logging for SignalR.

src: Handle errors in ASP.NET Core Blazor apps: Detailed circuit errors

{
  "DetailedErrors": true, // turns on CircuitOptions.DetailedErrors
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information",
      "Microsoft.AspNetCore.SignalR": "Debug"  // turns on SignalR debugging
    }
  }
}
Tyson Gibby
  • 2,590
  • 2
  • 18
  • 37
19

A better way to add detailed errors is to check your environment first. In Startup.cs add IWebHostEnvironment env to your constructor.

Then you can do this:

services.AddServerSideBlazor().AddCircuitOptions(o =>
{
    if (_env.IsDevelopment()) //only add details when debugging
    {
        o.DetailedErrors = true;
    }
});
Eonasdan
  • 7,563
  • 8
  • 55
  • 82
  • That's a really good point and an important one from a security point of view. I've updated my answer accordingly – tomRedox Sep 17 '19 at 13:18
4

For .NET Core 6 you can use WebApplicationBuilder.

var builder = WebApplication.CreateBuilder(args);
if (builder.Environment.IsDevelopment())
{
    builder.Services.AddServerSideBlazor().AddCircuitOptions(x => x.DetailedErrors = true);
}
else
{
    builder.Services.AddServerSideBlazor();
}
John81
  • 3,726
  • 6
  • 38
  • 58
3

For me it was slightly different

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseSetting(WebHostDefaults.DetailedErrorsKey, "true");
            webBuilder.UseStartup<Startup>();
        });
Alvaro Rivoir
  • 401
  • 3
  • 9