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:
Install-Package Elmah.Io.Extensions.Logging
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 LoggerFactory
:
var loggerFactory = new LoggerFactory()
.AddElmahIo("API_KEY", new Guid("LOG_ID"));
Replace API_KEY
with your API key (Where is my API key?) and LOG_ID
with the log ID (Where is my log ID?) that should receive errors from Entity Framework.
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.
See how we can help you monitor your website for crashes Monitor your website