0

I want to achieve the following:

  1. Have custom log statements in my ASP.NET Core web service application.
  2. Deploy my application to Azure (in my case using Pulumi).
  3. Call the webservice so it triggers the logging code.
  4. Read the logged messages, either programmatically or via the Azure browser-based GUI.

I am targeting .NET 5.0.

In my code I do something like this:

public class MyController : ControllerBase
{
    private readonly ILogger<MyController> _logger;
    public MyController(ILogger<MyController> logger) => _logger = logger;

    public async Task<ActionResult<something>> DoStuff()
    {
        _logger.LogInformation("Hello, World!");
        ...
    }
}

My Pulumi code contains this:

    var app = new AppService(
        "kmsApp",
        new AppServiceArgs
        {
            Logs = new AppServiceLogsArgs
            {
                ApplicationLogs = new AppServiceLogsApplicationLogsArgs { FileSystemLevel = "Error" },
                DetailedErrorMessagesEnabled = true,
                FailedRequestTracingEnabled = true,
                HttpLogs = new AppServiceLogsHttpLogsArgs
                {
                    FileSystem = new AppServiceLogsHttpLogsFileSystemArgs { RetentionInDays = 1, RetentionInMb = 35 }
                }
            }
        },
        ...);

With the above, when I am running my application in debug mode in Visual Studio, I can see the log messages in the Output pane. So the logging code definitely gets triggered. But when I deploy my application to Azure, I don't know how to get the log messages, and I find the Azure GUI confusing.

What I am struggling with is this:

  1. What configuration do I need to do in my code - e.g. NuGet packages or stuff in my Program and Startup classes?
  2. What configuration do I need to do in Azure?
  3. Where in the Azure browser-based GUI do I go to see these log messages?
  4. How can I fetch these logs programmatically (either via Pulumi or the raw Azure API)?

I have looked for documentation, of course, but I find the documentation labyrinthine. Most of it seems to be about diagnostics such as response time. I just want to view my own custom log messages from my code...

Posts like this one give some hints, but after reading the thread it is still nebulous to me how to read the logs: ASP.NET Core trace logging on Azure with Application Insights

There probably exists good documentation and guides. Please help me find them.

Thanks in advance!

Claus Appel
  • 1,015
  • 10
  • 28
  • 1
    You can read the logs after clicking on (For example) Performances → Choose a call → Look at the bottom right "View All Telemetry". If the logs didn't appear, [ensure that your appsettings log filters](https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net-core#how-do-i-customize-ilogger-logs-collection) are configured to send the level of logs you wish. – Emy Blacksmith Feb 19 '21 at 13:22
  • I don't see that "View All Telemetry" button. Can you please explain again what sequence of things you click on to get that button? – Claus Appel Feb 22 '21 at 07:16

2 Answers2

2

You have several options:

  1. Use az webapp log command from PowerShell to configure your application to log to files
  2. Use az webapp log tail command from PowerShell to see the logs in real time
  3. Configure the application logging manually from the Azure Portal
  4. Enable Application Insights for app service

For downloading the logs use the az webapp log download command or connect to the logs directory with FTP

Igor
  • 266
  • 1
  • 3
  • 13
  • And there is one more option to view the logs in real-time(no need to manually use the **az webapp log tail** command). Go to Azure Portal -> Monitoring(left menu) -> Log stream. – Igor Feb 19 '21 at 14:30
0

I think I figured out what was missing. I did two things.

First thing was to change my error level in Pulumi from "Error" to "Verbose":

ApplicationLogs = new AppServiceLogsApplicationLogsArgs { FileSystemLevel = "Verbose" },

The other thing was to install a site extension:

  1. Go to app service in Azure.
  2. In the left-hand menu under Monitoring go to App Service logs.
  3. Click the banner that says Click here to install the ASP.NET Core site extensions to enable Application Logging.

After this I was able to see logs by running az webapp log tail as suggested by Igor.

Now I just need to figure out how to do this programmatically with Pulumi.

enter image description here

Claus Appel
  • 1,015
  • 10
  • 28