1

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

I would recommend using Application Insights, for .NET based apps it provides an excellent way for you to do complete APM. If you want to use with ILogger then please take a look here. If you want to get started without ILogger then take a look here.

4 Comments

Thanks but I don't want to use Application Insights. At least not yet. I would think there is no reason I should since the logging works fine for my API. I was also just able to get this to work. I will post an answer in a bit.
I understand just to use logging but app insights at the same time not only provides logging, but it gives you the ability to do logging + insights on the data being logged. it is an APM tool at the end of the day.
Thanks. Setting up, reviewing, and understanding App Insight is next on the list. I just wanted to set up logging/tracing since it is the MVP for this delivery. Thanks for your feedback.
@HassanRaza Unfortunately, for a client-side Blazor application App Insights doesn't seem to work. I know the question was for a server-side app but just want people to know that client-side doesn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.