How to implement logging for Razor & Blazor Server Apps?

Siegfried Heintze 1,861 Reputation points
2022-12-04T14:15:15.117+00:00

I've been experimenting application logging with a small C# Blazor Server App and I'd like some help selecting from all the options offered for logging.

Here is my experimental code:

@inject ILogger<Index> logger  
<button @onclick="makeLogEntry">  
    Press me to make a log entry!  
</button>  
<div style="border:groove">  
    count = @count  
</div>  

@code{  
   int count = 0;  
   async Task makeLogEntry(){  
        new TelemetryClient(configuration).TrackTrace("elemetryClient");  
       System.Console.WriteLine("System.Console.WriteLine Button press " + now);  
       System.Diagnostics.Trace.WriteLine($"System.Diagnostics.Trace.WriteLine: {now}");  
       logger.LogInformation("logger.LogInformation: Button press " + now);  
      System.Diagnostics.Trace.TraceInformation($"System.Diagnostics.Trace.TraceInformation: {now}");  
   }  
}  

I can do kusto queries from

(a) The Application Insights azure object
(b) The Log Analytics azure objects
(c) The azure Monitor

Questions:

  1. What is the difference between doing kusto queries on the Application Insights object and the Log Analytics object and azure Monitor?
  2. I've been reading here (troubleshoot-diagnostic-logs) about using System.Diagnostices.Trace and here (console) about using application insights. How do I choose? Are these approaches redundant? And what about System.Console.WriteLine? Should I not use that? Looks like the ASP.NET logger can be redundant with using the telemetry client directly. Is this true?
  3. How do I choose between doing my kusto queries in the application insights azure object, the log analytics objects and the azure monitor? Are these all redundant?
  4. I see the portal.azure.com gives me the option of sending my logs to azure (blob) storage. How do I query my log data after it is stored in azure storage? Can I use kusto queries on the storage account?

Thanks

Siegfried

Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
2,807 questions
0 comments No comments
{count} votes

Accepted answer
  1. AnuragSingh-MSFT 19,936 Reputation points
    2022-12-11T10:37:58.17+00:00

    Hi @Siegfried Heintze ,

    Thank you for showing interest in Application Insights for your application. Please find below the response to your queries. (In your question, by Log Analytics - you are referring to Log Analytics workspace. Log Analytics is a tool in the Azure portal to edit and run log queries against this workspace)

    1. Log Analytics workspace is the storage environment for storing log and other monitoring data collected from various sources - ApplicationInsights, Microsoft Sentinel, Microsoft Defender for Cloud etc. Therefore, you can think of it has the backend storage where all the information collected by ApplicationInsights is stored.

    Whenever you run a Kusto query - either in Azure Monitor, Log Analytics Workspace or from Application Insights, they all run on the Logs stored in "Log Analytics Workspace" as available from the scope (shown below).

    269293-image.png

    2. Logging providers store logs, except for the Console provider which displays logs. Therefore, System.Console.WriteLine could be useful when you are working with console application with attached console which will output the log messages. However, for WebApplication Razor and Blazor pages, this will not be ideal - as most of the time the console will not be available to view output.
    System.Diagnostices.Trace - generally writes the logs to log file of the application. There are 2 ways that the traces from this module can be send to "Log Analytics Worksapce"

    1. Send to LogAnalytics workspace using Diagnostic Settings available in App Service in Azure portal
    2. If you enable "Application Insights" on the App Service directly, it can listen to these traces without any additional configuration. Please see this link for details - Explore .NET/.NET Core and Python trace logs in Application Insights

    I would recommend using the Ilogger method as it more convenient and can be easily used with dependency injection to meet the requirement.

    3. They all query the same datasource and present the same result. Please see answer 1 for more details. These are some of the different methods for querying same underlying data.

    4. No, you would not be able to use Kusto directly on Blob storage. Blob storage stores details in blobs (similar to files on disk). You may use Azure Storage Explorer to view the contents of blobs in the storage account. There are other ways to query/analyze blob content (using Synapse, Data Explorer etc), however the basic use case of storing logs in Blob is to retain it for longer duration.
    The data contained in Log Analytics workspace (collected through Application Insights) is much easier to run queries and get results.

    Please let me know if you have any questions.

    ---
    Please 'Accept as answer' if it helped so that it can help others in the community looking for help on similar topics.


0 additional answers

Sort by: Most helpful