Search code examples
azureazure-web-app-serviceblazorblazor-server-sideazure-appservice

How can I add tracing / logging messages for a Blazor Server Side App on Azure? (Preferred with App Service Logs)


I have a Blazor Server Side App that runs on Azure. I want to add tracing / logging messages (_logger.LogInformation()). I would prefer to use Azue App Service Logs. But, I am open to other options.

I was able to get tracing / logging messages working with an API written in .Net Core that runs on Azure. These logs are written to Azure App Service Logs. Their type are Application.

For my Blazor App, I followed the same steps setting up tracing / logging as I did with my API. However, when I check the log files in Cloud Explorer no Application folder, under the LogFiles folder is created.

I made sure I turned Azure App Service Logs and set the correct Level. See below.

enter image description here

My Program.cs uses the default setting. Which I read should automatically set up logging. (It did from my API) See below.

enter image description here

Below is the example of the code I added to do the tracing / logging.

public class VpbDelegateAdminService : IVpbDelegateAdminService
{
    private readonly HttpClient _httpClient;
    private readonly IJsonSerializerWrapper _jsonSerializerWrapper;
    private readonly TokenProvider _tokenProvider;
    private readonly ILogger<VpbDelegateAdminService> _logger;

    public VpbDelegateAdminService(HttpClient httpClient, IJsonSerializerWrapper jsonSerializerWrapper, TokenProvider tokenProvider, ILogger<VpbDelegateAdminService> logger)
    {
        _httpClient = httpClient;
        _jsonSerializerWrapper = jsonSerializerWrapper;
        _tokenProvider = tokenProvider;
        _logger = logger;
    }
    
    public async Task<VpbDelegateListVm> GetVpbDelegatesAsync(int pageNo, string searchText)
    {
        _logger.LogInformation($"Argument(s)- pageNo: {pageNo}, searchText: {searchText ?? "null"}");

As I mentioned above, I would prefer to use Azure App Service Logs. But, if that is not possible with Blazor or if someone has had success with other options to use with Blazor, I am interested to hearing about them.

Thanks for your help.


Solution

  • I figured it out myself.

    I was able to get Logging / Tracing working with my Blazor server side app using App Service Logs by following the steps here: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-3.1 Related to steps for: AzureAppServices

    Steps (note: There steps are only for filesystem / filestream. I didn't set up blob):

    1. Update appsettings.json with:

        "AzureAppServicesFile": {
            "IncludeScopes": true,
            "LogLevel": {
                "Default": "Warning"
            }
        }
    

    2. Install nuget packages for Microsoft.Extensions.Logging.AzureAppServices

    3. Update the Program.cs with the following code:

    public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)
                    .ConfigureLogging(logging => logging.AddAzureWebAppDiagnostics())
                    .ConfigureServices(serviceCollection => serviceCollection
                        .Configure<AzureFileLoggerOptions>(options =>
                        {
                            options.FileName = "diagnostics-";
                            options.FileSizeLimit = 50 * 1024;
                            options.RetainedFileCountLimit = 5;
                        }))
                    .ConfigureWebHostDefaults(webBuilder =>
                    {
                        webBuilder.UseStartup<Startup>();
                    });
    

    4. Turned on App Service Log for Application Logging (Filesystem)

    5. Set the level to "Information"

    And my logs / tracing (see below) started showing up in Cloud Explorer

    logger.LogInformation($"Argument(s)- pageNo: {pageNo}, searchText: {searchText ?? "null"}");
    

    I hope these steps help someone else.