How to Integrate Azure Application Insights with Azure Functions Using .NET 8 in C#
— Azure, Code, .NET — 2 min read
How to Integrate Azure Application Insights with Azure Functions Using .NET 8 in C#
Monitoring and diagnosing the performance of your serverless applications is crucial. Azure Application Insights provides extensive monitoring capabilities, making it easier to track the health and performance of your Azure Functions. This post will guide you through integrating Azure Application Insights with Azure Functions using .NET 8 in C#.
Setting Up Azure Application Insights
If you haven't already, create an Azure Application Insights resource.
Create an Azure Functions with .NET 8 and Application Insights
Create an Azure Functions Project
Create a new Azure Functions project using .NET 8.
dotnet new azurefunctions --name MyFunctionApp --framework net8.0cd MyFunctionApp
Add Application Insights SDK
Install the Application Insights SDK for Azure Functions:
dotnet add package Microsoft.Azure.Functions.Extensionsdotnet add package Microsoft.ApplicationInsights.WorkerServicedotnet add package Microsoft.Extensions.Logging.ApplicationInsights
Configure Application Insights
Update the local.settings.json
file to include the Application Insights instrumentation key:
{ "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "UseDevelopmentStorage=true", "FUNCTIONS_WORKER_RUNTIME": "dotnet", "APPLICATIONINSIGHTS_CONNECTION_STRING": "APPINSIGHTS_CONNECTION_STRING" }}
Update the host.json file to configure logging:
{"version": "2.0","logging": { "applicationInsights": { "samplingSettings": { "isEnabled": true } } }}
Update Program.cs
Modify Program.cs
to configure Application Insights for in-process functions
using Microsoft.Extensions.Hosting;using Microsoft.Extensions.DependencyInjection;using Microsoft.Extensions.Logging;
var host = new HostBuilder() .ConfigureFunctionsWorkerDefaults() .ConfigureServices(s => { s.AddApplicationInsightsTelemetryWorkerService(); s.Configure<Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions>(options => { options.ConnectionString = Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING"); }); }) .ConfigureLogging(logging => { logging.AddApplicationInsights(); logging.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Information); }) .Build();
host.Run();
Implement a Sample Function
Create a sample HTTP-triggered function Function1.cs
using Microsoft.Azure.Functions.Worker;using Microsoft.Azure.Functions.Worker.Http;using Microsoft.Extensions.Logging;using System.Threading.Tasks;using System.Net;
namespace MyFunctionApp{ public class Function1 { private readonly ILogger _logger;
public Function1(ILoggerFactory loggerFactory) { _logger = loggerFactory.CreateLogger<Function1>(); }
[Function("Function1")] public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req) { _logger.LogInformation("C# HTTP trigger function processed a request.");
var query = System.Web.HttpUtility.ParseQueryString(req.Url.Query); string name = query["name"];
var response = req.CreateResponse(HttpStatusCode.OK); if (!string.IsNullOrEmpty(name)) { response.WriteString($"Hello, {name}"); } else { response.WriteString("Please pass a name on the query string or in the request body"); }
return response; } }}
Deploy to Azure
Deploy your Azure Functions app to Azure1
func azure functionapp publish <YourFunctionAppName>
Conclusion
By following these steps, you have successfully integrated Azure Application Insights with your Azure Functions app using .NET 8 in C#. This setup will help you monitor your functions, diagnose issues, and gain insights into the performance of your serverless applications.
Feel free to try it and happy coding!