I am currently working with an ASP.NET Core v6 Function App v4 and want to add logging so I can save the logs to the database.
In past projects I’ve used Log4Net package, I see there is a nuget package that allows us to use it for ASP.NET Core v6, but it’s mostly for web and API projects with Program.cs
files. The Function App only comes with a Startup.cs
and doesn’t allow you to add ILogger
.
I am wondering if there is a way or a work around in adding this tool or logging to the project in general or what other options there are our there.
Here is the link to the repo/docs for the nuget package.
Microsoft.Extensions.Logging.Log4Net.AspNetCore
And the original Log4Net site:
Added like so and get the error:
IFunctionHostBuilder does not contain a definition of ‘Logging’ and no accessible extension methedon ‘Logging’ accepting a first argument of type ‘IFunctionsHostbuilder’ could not be found (are you missing a using dir or an assembly ref)?
Code:
public override void Configure(IFunctionsHostBuilder builder)
{
//...
builder.Logging.AddLog4Net();
//...
}
I’m pretty new to the Function apps so any guidance would be grateful.
2
Answers
You can get rid of
Startup.cs
file in favor of one-fileProgram.cs
. Configuration is then almost the same as in ASP.NET Core appFunction
UPD:
Your csproj file should contain:
To make app isolated, set configuration value in local.settings.json
and in Azure
You may consider serilog, will be much more easier to use for non-isolated function. No startup.cs/program.cs or other configuration needed.
Create a project like below.
Install package
Serilog
Serilog.Sinks.Console``Serilog.Sinks.MSSqlServer
Modify the Function1.cs like below
Test
Check Sqlserver
Reference: https://stackify.com/logging-azure-functions/