'EntityFramework 5 Logging in Azure Functions
TLDR;
How can we make EF5 logging into its own category instead of the category of the Azure Function?
After we upgraded our Azure Functions from .NET core to .NET5/6 and respectively EF to EF5, we noticed that all DB context queries are ending up as (information) logs in the Azure Function category -and not in a dedicatedd category, i.e.Microsoft.EntityFrameworkCore. The host.json looks like this:
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingExcludedTypes": "Request",
"samplingSettings": {
"isEnabled": true
}
},
"logLevel": {
"Default": "Warning",
"Function": "Information",
"Microsoft.EntityFrameworkCore": "Warning",
}
}
}
The observed behavior is that the query of context.Employees.ToList() is logged as information:
[2022-04-27T08:00:16.362Z] Entity Framework Core 5.0.15 initialized 'MyDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: MaxBatchSize=100
[2022-04-27T08:00:17.896Z] Executed DbCommand (59ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
[2022-04-27T08:00:17.899Z] SELECT [p].[Name]
[2022-04-27T08:00:17.900Z] FROM [mdm].[Employees] AS [p]
If we change the log level of "Function" to warning, the logs disappear (together with all(!) other logs related to the function). To me, it seems that the default EF5 logger ignores the concept of log-categories and just writes into the standard one. Ideally, we wanted to log into dedicated category "Microsoft.EntityFrameworkCore": "Warning"
so that we can turn it on/off on a need basis.
We noticed that there is some simple logging change which can be influenced by db context options. Also using LoggerFactory doesn't have the concept of categories?
private static void AddDefaultContext(this IServiceCollection services, ServiceLifetime serviceLifetime = ServiceLifetime.Scoped)
{
services.AddDbContext<CreditRiskDashboardDbContext>(
s, options) =>
{
var config = s.GetService<IOptions<CreditRiskConfiguration>>().Value;
// This works, but overrides/ignores anything set in host.json
//options.UseLoggerFactory(LoggerFactory.Create(builder => builder.SetMinimumLevel(LogLevel.Warning)));
}, serviceLifetime);
}
The documentation doesn't speak to log categories either. So what to do?
Solution 1:[1]
There are few workarounds to use Logging in the Entity Framework 5.0 of Azure Functions where they used DB Context Queries :
Sample code:
var user = new ApplicationUser { FirstName = "Azure", LastName = "Kontext" };
dbContext.Add(user);
dbContext.SaveChanges();
/*Now read the users from the in memory database*/
var users = dbContext.ApplicationUsers;
foreach (var u in users)
{
logger.LogCritical($"FirstName={u.FirstName}, LastName={u.LastName}");
}
DBContext in Azure Function:
public Function1(IOptions<AppConfig> appConfig**, ApplicationDbContext dbContext**)
{
this.appConfig = appConfig;
this.dbContext = dbContext;
}
Please check the below practical solutions shows logging w.r.t EF5 in Azure Functions using DBContext:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | HarshithaVeeramalla-MT |