Introduction
Logging in Azure Functions written in C# is crucial for monitoring and troubleshooting your server less applications. Azure Functions supports various logging techniques to capture and store information about application behavior and execution.
First start to create a service and then register them in a startup class like below.
Implementation
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
[assembly: FunctionsStartup(typeof(LoggingDemo.Startup))]
namespace LoggingDemo;
class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddSingleton<SampleService>();
}
}
We also need to add Microsoft.Azure.Functions.Extensions from Nuget. Then in my SampleService I would inject a ILogger to use for native logging.
Example :
using Microsoft.Extensions.Logging;
namespace LoggingDemo;
public class SampleService
{
private ILogger<SampleService> _logger;
public SampleService(ILogger<SampleService> logger)
{
_logger = logger;
}
public void DoSomething()
{
_logger.LogInformation("I am logging sample information.");
}
}
To use the service in a Function it’s important to make a function “non-static” and then inject the service:
namespace LoggingDemo;
public class HelloWorldWithDI
{
private readonly SampleService _sampleService;
public HelloWorldWithDI(SampleService sampleService)
{
_sampleService = sampleService;
}
[FunctionName("HelloWorldWithDI")]
public IActionResult Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
_sampleService.DoSomething();
return new OkResult();
}
}
Calling this function will only output the log of the function but not the log “I am logging sample information.” from the service although the service is being called. That’s because by default the runtime only outputs logs of a function. To change this let’s enable the log level “Information” as default in the host.json for all our code:
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
},
"logLevel": {
"default": "Information"
}
}
}
It’s also possible to reduce this to just the log of specific classes. Therefore we need to change in a function for the generic Logger to ILogger of the class.
namespace LoggingDemo;
public class HelloWorldWithDI
{
private readonly SampleService _sampleService;
private readonly ILogger<HelloWorldWithDI> _logger;
public HelloWorldWithDI(SampleService sampleService, ILogger<HelloWorldWithDI> logger)
{
_sampleService = sampleService;
_logger = logger;
}
[FunctionName("HelloWorldWithDI")]
public IActionResult Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req)
{
_logger.LogInformation("C# HTTP trigger function processed a request.");
_sampleService.DoSomething();
return new OkResult();
}
}
And then specify the classes we want to be logged out in the host.json
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
},
"logLevel": {
"default": "None",
"LoggingDemo.HelloWorldWithDI": "Information",
"LoggingDemo.SampleService": "Information"
}
}
}
Log Levels and Usage
- LogInformation: General information messages.
- LogError: Errors that might still allow the function to continue.
- LogWarning: Issues that should be addressed but won’t stop the function.
- LogCritical: Critical failures that require immediate attention.
- LogDebug: Debugging information for developers.
Viewing Logs
Azure Functions logs are available in several places:
- Azure Portal: Navigate to function app, select the function, and go to the “Monitor” tab to view logs.
- Application Insights: Integrated logging and monitoring solution for more advanced logging and analytics.
- Streaming Logs: Use Azure CLI or Azure PowerShell to stream logs directly to the console.
Conclusion
Logging in Azure Functions helps in debugging, monitoring, and understanding the behavior of your serverless applications. It’s essential to use structured logging (ILogger) to capture context-specific information and integrate with Azure Monitor or Application Insights for advanced analytics and monitoring capabilities.