I am trying a fairly easy thing to do, but somehow I am missing something after all the Microsoft documents and SO posts I've been reading today.
I want to do some proper logging for my Azure Functions and all over the internet the best practice looks like it's Application Insights, but I've been struggling to get my AI to log my custom logs.
Here is my test function, if needed I can create a test project also.
Host.json
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": false
},
"logLevel": {
"default": "Information",
"Minerva.Clocking": "Information"
}
}
}
}
Startup.cs
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
ServiceProvider serviceProvider = builder.Services.BuildServiceProvider();
Configurations.StaticConfig = serviceProvider.GetService<IConfiguration>();
ConfigureServices(builder.Services);
}
private static void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IEmailService, EmailService>();
services.AddHttpClient();
}
}
My test function
namespace Minerva.Clocking.Endpoints
{
public class LoggingTestFunction
{
private readonly IEmailService m_EmailService;
public LoggingTestFunction(IEmailService emailService)
{
m_EmailService = emailService;
}
[FunctionName("LoggingTestFunction")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation($"C# HTTP trigger function processed a request. Execution time: {DateTime.Now}");
log.LogInformation("Info from function");
log.LogWarning("Warning from function");
log.LogError("Error from function");
m_EmailService.LogSimpleMessage();
m_EmailService.Foo();
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
string responseMessage = string.IsNullOrEmpty(name)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {name}. This HTTP triggered function executed successfully.";
return new OkObjectResult(responseMessage);
}
}
}
My EmailService
namespace Minerva.Clocking.Services
{
public class EmailService : IEmailService
{
private readonly HttpClient m_httpClient;
private readonly ILogger<EmailService> m_EmailLogger;
public EmailService(IHttpClientFactory httpClientFactory, ILogger<EmailService> logger)
{
m_httpClient = httpClientFactory.CreateClient();
m_EmailLogger = logger;
}
public void LogSimpleMessage()
{
m_EmailLogger.LogInformation("This is a information to be seen in azure");
m_EmailLogger.LogError("This is a error to be seen in azure");
m_EmailLogger.LogWarning("This is a warning to be seen in azure");
}
public void Foo()
{
m_EmailLogger.LogInformation("Foo");
m_Telemetry.TrackTrace("BarLog", Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Information);
m_Telemetry.TrackEvent("BarEvent");
}
}
}
Whenever I look into the AI logs in azure all I see is the message logged inside the function itself and nothing from my EmailService
Most likely I am missing something, but I am not sure what, can someone point me to a documentation or something that could allow me to log everything that is inside my Azure Function in AI?
I would also like to be able to see the logs in console, but I couldn't find any relevant information about that either on the internet (I know I am a bad searcher).
Thank you
Edit : Updated the code to include ClientTelemetry. The log from telemetry I can see in Azure, but still no logs from ILogger.