How can I view console or trace output in an azure app service? Console.WriteLine or Trace.TraceError

12,357

Solution 1

I finally figured it out myself. I needed to add configuring AzureFileLoggerOptions. Without those options, nothing showed up in the Azure output.

.ConfigureLogging(logging => logging.AddAzureWebAppDiagnostics())
.ConfigureServices(serviceCollection => serviceCollection
    .Configure<AzureFileLoggerOptions>(options =>
    {
        options.FileName = "azure-diagnostics-";
        options.FileSizeLimit = 50 * 1024;
        options.RetainedFileCountLimit = 5;
    }).Configure<AzureBlobLoggerOptions>(options =>
    {
        options.BlobName = "log.txt";
    })
)

In summary, for .Net Core 3.1, to get your messages to show up in the "Log Stream", you need to enable "Application Logging" in the "App Service Logs" section of Azure. In your code you need to reference:

using Microsoft.Extensions.DependencyInjection;

using Microsoft.Extensions.Logging.AzureAppServices;

Also, in my case, I am using Blazor/SignalR and don't have an MVC Controller, so I couldn't figure out how to access the Logging framework in my class. I am sure there is a better way, but by exposing the ILogger as in the code below I was able to reference it from anywhere within my code base.

I have a static method I call to write to the log (console when I am actively debugging on my pc, or Azure "Log Stream" when running in Azure:

public static void WriteToLog(string message)
{
    Program.Logger.LogError(message);
}

Code in my Program.cs looks like this:

public class Program
    {
        public static ILogger Logger;
        public static void Main(string[] args)
        {
            //CreateHostBuilder(args).Build().Run();

            var host = CreateHostBuilder(args).Build();

            var LoggerF = LoggerFactory.Create(builder =>
            {
                builder.AddFilter("BlazorDiceRoller", LogLevel.Warning)
                .AddConsole().AddAzureWebAppDiagnostics()
                .AddFilter("Microsoft", LogLevel.Warning)
                .AddFilter("System", LogLevel.Warning);
            });
            Logger = LoggerF.CreateLogger<Program>();
            host.Run();
        }

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

Solution 2

What I did was just to enable the App Service Logs in the Azure Portal.

Specifically I turned on the "Application Logging (Filesystem)" at the "Detailed" Level for this purpose and then I selected "File System" in the "Web server logging" option

Then you can use the "Log stream" in the left bar to display the application logs.

In your code you just have to call

System.Diagnostics.Trace.TraceInformation("My message!")

Or

System.Diagnostics.Trace.WriteLine("My message!")

And that's it!

Here I include some screenshots:

Screenshot of the selected options

Log Stream

Share:
12,357

Related videos on Youtube

Rob Kraft
Author by

Rob Kraft

Updated on June 04, 2022

Comments

  • Rob Kraft
    Rob Kraft almost 2 years

    I thought this would be simple but after several hours have realized that it is not. I deployed an "App Service" to Azure that is a C# .Net Core.

    I am trying to add some crude monitoring of by app by using Console.WriteLine("my message") or Trace.TraceError("my message") but I can't find that output anywhere in Azure.

    I tried enabling Application Insights but didn't find the output there either.

    I just want the simplest way to get a little idea that a line of code in my app is getting hit. What is that simplest way?

    Things I have tried: 1) I went in to the console in Azure and browsed to every file that might have this sort of output but did not find any. Certainly nothing in any of the files and folders under LogFiles. 2) I DID go to "App Service Logs" and enabled "Application Logging (Filesystem)". 3) I also enabled "Detailed error messages" in "App Service Logs". 4) I tried "Diagnostic settings (preview)" but could not find the output there. 5) I watch the "Log Stream" for Application Logs but nothing shows up there 6) Under "Logs" I just have this message: "We didn’t find any logs" 7) Neither "metrics" nor "Alerts" has this log output

    I am beginning to suspect this is not support in Azure. Do I need to add a logging framework like serilog just to one time add a statement to may app like "You hit line 20"? I really just want something quick and dirty, but after spending a few hours on this the "quick" part of it isn't happening.

  • Rob Kraft
    Rob Kraft about 4 years
    Thanks for responding Ivan. I am using Blazor with SignalR and don't have a controller, so I don't know the best way to grab the logger. I figured out why it was not working for me, and you can see my solution in the answer I posted to my own question.