Why am I not getting Trace logging on Azure App Server

95 views Asked by At

I am using the same log settings on my dev system and on Azure. Both use appsettings.json. I have no Logger settings in appsettings.Development.json or secrets.json on my dev system. I have none in the Environment Variables on Azure.

My logs on my dev system show LogTrace() (where specified). But on Azure it does not. It has DEBUG, but not TRACE.

Is there some override I need to set in Azure to allow Trace logging?

This is not for the app service logs in Azure, it is for an ILoggerProvider I have written myself.

1

There are 1 answers

4
Aslesha Kantamsetti On

I'm using the following code to implement a custom ILogger in an ASP .NET Core 6 web application.

To observe logs in Azure, I added the following code to the Program.cs file.

program.cs:

builder.Services.AddSingleton<ILoggerProvider, CustomLoggerProvider>();
builder.Logging.AddAzureWebAppDiagnostics();
builder.Services.Configure<AzureFileLoggerOptions>(options =>
{
    options.FileName = "LogTracestoFile";
    options.FileSizeLimit = 50 * 1024;
    options.RetainedFileCountLimit = 3;
});

I added the following NuGet package to the .csproj file.

.csproj:

<PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" Version="8.0.2" />

This is my customLogger class.

CustomLogger.cs:

using Microsoft.Extensions.Logging;
using System;
namespace iloggerwebapp
{
    public class CustomLogger : ILogger
    {
        public IDisposable BeginScope<TState>(TState state) => null;
        public bool IsEnabled(LogLevel logLevel) => true;
        public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
        {
            Console.WriteLine($"[{logLevel}] - {formatter(state, exception)}");
        }
        public void LogTrace(string message)
        {
            Log(LogLevel.Trace, new EventId(), message, null, (state, exception) => state.ToString());
        }
    }
}

CustomLoggerProvider.cs:

using Microsoft.Extensions.Logging;
namespace iloggerwebapp
{
    public class CustomLoggerProvider : ILoggerProvider
    {
        public ILogger CreateLogger(string categoryName)
        {
            return new CustomLogger();
        }
        public void Dispose()
        {
        }
    }
}

HomeController.cs:

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using iloggerwebapp;
namespace iloggerwebapp.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }
        public IActionResult Index()
        {
            _logger.LogTrace("This is a trace message from HomeController.");
            _logger.LogInformation("this is a information from ILogger...");
            return View();
        }
    }
}

appsettings.json::

{
  "Logging": {
    "LogLevel": {
      "Default": "Trace",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}

appsettings.Development.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Trace",
      "Microsoft.AspNetCore": "Warning"
    }
  }
}

This is my local console Log trace:

enter image description here

After publishing the web app to Azure App Service, navigate to Monitoring -> App Service Logs, and turn on Application Logging (Filesystem) and set Level to Verbose as shown below.

enter image description here

I was also able to see the logs in the Log Stream in the Azure Web App, as shown below.

enter image description here

Azure App service Output:

enter image description here