Logging to elmah.io from Entity Framework Core
Both elmah.io and Entity Framework Core supports logging through Microsoft.Extensions.Logging. To log all errors happening inside Entity Framework Core, install the Elmah.Io.Extensions.Logging NuGet package:
dotnet add package Elmah.Io.Extensions.Logging
<PackageReference Include="Elmah.Io.Extensions.Logging" Version="4.*" />
paket add Elmah.Io.Extensions.Logging
Then add elmah.io to a new or existing
var loggerFactory = new LoggerFactory() .AddElmahIo("API_KEY", new Guid("LOG_ID"));
When using Entity Framework Core from ASP.NET Core, you never create a
LoggerFactory. Factories are provided through DI by ASP.NET Core. Check out this sample for details.
Finally, enable logging in Entity Framework Core:
optionsBuilder .UseLoggerFactory(loggerFactory) .UseSqlServer(/*...*/);
UseSqlServer included for illustration purposes only - elmah.io works with any provider)
That's it! All errors happening in Entity Framework Core, are now logged in elmah.io.
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.