I created a Azure functions (HTTP Trigger) v4 that writes data to an Azure Table storage.
Heres the code for my Azure function (C# .NET 6, non isolated):
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="5.0.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Tables" Version="1.2.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.2.0" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.Storage.Table;
using System.Threading.Tasks;
using System;
public static class Function1
{
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
[Table("tablename", Connection = "AzureWebJobsStorage")] IAsyncCollector<UserLogEntry> userLogsTable,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
try
{
// Process data...
await userLogsTable.AddAsync(logEntry);
log.LogInformation("Log entry added successfully.");
return new OkObjectResult("Log entry added successfully.");
}
catch (Exception ex)
{
log.LogError(ex, "An error occurred while processing the request.");
return new StatusCodeResult(500); // Internal Server Error
}
}
}
public class UserLogEntry : TableEntity
{
public UserLogEntry(string partitionKey)
{
PartitionKey = partitionKey;
RowKey = partitionKey + "-" + Guid.NewGuid().ToString();
}
// Data properties
}
I can test this locally using Azure Storage emulator (connecting via localhost) and can see the table "tablename" being populated with data as its being written to it. I then deployed this Azure function and set it as managed identity and gave it Table Storage contributor to my table. Everything was successful. I trigger the function from my local app and can see in the monitor its being triggered. I look at the logs and can see everything is OK, no errors.
2023-09-14T16:21:13Z [Information] Executing 'xxx' (Reason='This function was programmatically called via the host APIs.', Id=1f419d7c-67b4-4df1-b9d1-fda6a67acc5f)
2023-09-14T16:21:13Z [Information] C# HTTP trigger function processed a request.
2023-09-14T16:21:13Z [Information] Log entry added successfully.
However, when checking the table, its unchanged and still empty. I dont know if its an access issue, or an issue with my function code. I am getting the "Log Entry added successfully" print statement which leads me to believe there were no errors.
How do I debug the issue? I am not sure where to start looking.