You can add the default log level using **host.json** file and making the below changes in **program.cs** file.
```json
{
"version": "2.0",
"logging": {
"logLevel": {
"Function.Function1": "Information"
}
}
}
```
```csharp
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureServices(services =>
{
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
services.Configure<LoggerFilterOptions>(options =>
{
LoggerFilterRule defaultRule = options.Rules.FirstOrDefault(rule => rule.ProviderName == "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");
if (defaultRule is not null)
{
options.Rules.Remove(defaultRule);
}
});
})
.ConfigureAppConfiguration((hostContext, config) =>
{
config.AddJsonFile("host.json", optional: true);
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddApplicationInsights(console =>
{
console.IncludeScopes = true;
});
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
})
.Build();
host.Run();
```
I am using default .Net8 isolated function code as given below.
```csharp
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
namespace _79026563
{
public class Function1
{
private readonly ILogger<Function1> _logger;
public Function1(ILogger<Function1> logger)
{
_logger = logger;
}
[Function("Function1")]
public IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req)
{
_logger.LogInformation("C# HTTP trigger function processed a request.");
return new OkObjectResult("Welcome to Azure Functions!");
}
}
}
```
I am getting all the information logs as given below and it does work.
![enter image description here](https://i.imgur.com/CyuoD9O.png)
```csharp
Azure Functions Core Tools
Core Tools Version: 4.0.6280 Commit hash: N/A +421f0144b42047aa289ce691dc6db4fc8b6143e6 (64-bit)
Function Runtime Version: 4.834.3.22875
[2024-09-26T12:36:15.636Z] Found C:\Users\*****\79026563\79026563.csproj. Using for user secrets file configuration.
[2024-09-26T12:36:18.749Z] Azure Functions .NET Worker (PID: 38560) initialized in debug mode. Waiting for debugger to attach...
[2024-09-26T12:36:18.794Z] Worker process started and initialized.
Functions:
Function1: [GET,POST] http://localhost:7025/api/Function1
For detailed output, run func with --verbose flag.
[2024-09-26T12:36:23.854Z] Host lock lease acquired by instance ID '0000000000000000000000000D2022A4'.
[2024-09-26T12:36:37.667Z] Executing 'Functions.Function1' (Reason='This function was programmatically called via the host APIs.', Id=9e791713-f046-4d2c-adc1-8f6130e3ad21)
[2024-09-26T12:36:38.052Z] C# HTTP trigger function processed a request.
[2024-09-26T12:36:38.058Z] Executing OkObjectResult, writing value of type 'System.String'.
[2024-09-26T12:36:38.147Z] Executed 'Functions.Function1' (Succeeded, Id=9e791713-f046-4d2c-adc1-8f6130e3ad21, Duration=542ms)
```
>host.json with a log level of "Error" still logs Trace to Critical in application insights.
By using below code and configuration getting logs below log level which mentioned in host.json.
- I have created Http trigger function with runtime stack .NET 8.0 isolated.
- **The logs which trying to get particular function loglevel set in host.json like below:**
**host.json:**
```
{
"version": "2.0",
"logging": {
"logLevel": {
"Function.Function1": "Debug"
},
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
},
"enableLiveMetricsFilters": true
}
}
}
```
**Function1.cs:**
```
public class Function1
{
private readonly ILogger<Function1> _logger;
public Function1(ILogger<Function1> logger)
{
_logger = logger;
}
[Function("Function1")]
public IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
{
_logger.LogTrace("This is a TRACE log message");
_logger.LogDebug("This is a DEBUG log message");
_logger.LogInformation("This is a INFORMATION log message");
_logger.LogWarning("This is a WARNING log message");
_logger.LogError("This is a ERROR log message");
_logger.LogCritical("This is a CRITICAL log message");
_logger.LogInformation("C# HTTP trigger function processed a request.");
return new OkObjectResult("Welcome to Azure Functions!");
}
}
```
**Program.cs:**
```
var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureServices(services =>
{
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
services.Configure<LoggerFilterOptions>(options =>
{
LoggerFilterRule toRemove = options.Rules.FirstOrDefault(rule => rule.ProviderName
== "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");
if (toRemove is not null)
{
options.Rules.Remove(toRemove);
}
});
})
.ConfigureAppConfiguration((hostContext, config) =>
{
config.AddJsonFile("host.json", optional: true);
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddApplicationInsights(console =>
{
console.IncludeScopes = true;
});
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
})
.Build();
host.Run();
```
![enter image description here](https://i.imgur.com/nwyoYZZ.png)
- I have published the function into azure portal.
![enter image description here](https://i.imgur.com/S9Y8Pov.png)
- I have ran the function and got expected logs in azure portal as well. check below:
![enter image description here](https://i.imgur.com/WsOXe3P.png)
**Output:**
![enter image description here](https://i.imgur.com/8MS8FED.png)
![enter image description here](https://i.imgur.com/ftSlfZG.png)