ASP.NET Core Troubleshooting

So, your ASP.NET Core application doesn't log errors to elmah.io? We are here with a few things to try out.

  • Make sure to reference the most recent version of the Elmah.Io.AspNetCore NuGet package.
  • Make sure that the Elmah.Io.Client NuGet package is installed and that the major version matches that of Elmah.Io.AspNetCore.
  • Make sure that you are calling both the AddElmahIo- and UseElmahIo-methods in the Startup.cs file, as described on Logging to elmah.io from ASP.NET Core.
  • Make sure that you call the UseElmahIo-method after invoking other Use* methods that in any way inspect exceptions (like UseDeveloperExceptionPage and UseExceptionHandler).
  • Make sure that you call the UseElmahIo-method before invoking UseMvc.
  • Make sure that your server has an outgoing internet connection and that it can communicate with api.elmah.io on port 443. The integration for ASP.NET Core support setting up an HTTP proxy if your server doesn't allow outgoing traffic. Check out Logging through a proxy for details.
  • Make sure that you didn't enable any Ignore filters or set up any Rules with an ignore action on the log in question.
  • Make sure that you don't have any code catching all exceptions happening in your system and ignoring them (could be a logging filter, a piece of middleware, or similar).

Common exceptions and how to fix them

Here you will a list of common exceptions and how to solve them.

InvalidOperationException

Exception

[InvalidOperationException: Unable to resolve service for type 'Elmah.Io.AspNetCore.IBackgroundTaskQueue' while attempting to activate 'Elmah.Io.AspNetCore.ElmahIoMiddleware'.]
   Microsoft.Extensions.Internal.ActivatorUtilities+ConstructorMatcher.CreateInstance(IServiceProvider provider)
   Microsoft.AspNetCore.Builder.UseMiddlewareExtensions+<>c__DisplayClass4_0.<UseMiddleware>b__0(RequestDelegate next)
   Microsoft.AspNetCore.Builder.Internal.ApplicationBuilder.Build()
   Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

Solution

You forgot to call the AddElmahIo-method in the Startup.cs file:

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddElmahIo(o =>
    {
        ...
    });
    ....
}

ArgumentException

Exception

[ArgumentException: Input an API key Parameter name: apiKey]
   Elmah.Io.AspNetCore.Extensions.StringExtensions.AssertApiKey(string apiKey)
   Elmah.Io.AspNetCore.ElmahIoMiddleware..ctor(RequestDelegate next, IBackgroundTaskQueue queue, IOptions<ElmahIoOptions> options)
   Microsoft.Extensions.Internal.ActivatorUtilities+ConstructorMatcher.CreateInstance(IServiceProvider provider)
   Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
   Microsoft.AspNetCore.Builder.UseMiddlewareExtensions+<>c__DisplayClass4_0.<UseMiddleware>b__0(RequestDelegate next)
   Microsoft.AspNetCore.Builder.Internal.ApplicationBuilder.Build()
   Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

Solution

You forgot to call the AddElmahIo-method in the Startup.cs file:

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddElmahIo(o =>
    {
        ...
    });
    ....
}

or you called AddElmahIo without options and didn't provide these options elsewhere:

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddElmahIo();
    ....
}

Even though you configure elmah.io through appsettings.json you still need to call AddElmahIo. In this case, you can register ElmahIoOptions manually and use the empty AddElmahIo overload:

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.Configure<ElmahIoOptions>(Configuration.GetSection("ElmahIo"));
    services.AddElmahIo();
    ....
}

This article was brought to you by the elmah.io team. elmah.io is the best error management system for .NET web applications. We monitor your website, alert you when errors start happening and help you fix errors fast.

See how we can help you monitor your website for crashes Monitor your website