Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 771
Rob Kraft Avatar asked Mar 15 '20 19:03

Rob Kraft


People also ask

How do I view console logs in Azure App?

To enable application logging for Windows apps in the Azure portal, navigate to your app and select App Service logs. Select On for either Application Logging (Filesystem) or Application Logging (Blob), or both. The Filesystem option is for temporary debugging purposes, and turns itself off in 12 hours.

How do I debug Azure App Service in Visual Studio?

In Solution Explorer, right-click the project, and click Publish. In the Profile drop-down list, select the same profile that you used in Create an ASP.NET app in Azure App Service. Then, click Settings. In the Publish dialog, click the Settings tab, and then change Configuration to Debug, and then click Save.

How do I see Appinsight logs?

View logs in Application InsightsGo to Application Insights resource in your resource group. Go to Logs under Monitoring section. Click on traces eye button to get log traces. Select Time Range and click Run.

What are azure trace logs?

These logs come in .txt format and also contain any generic information that your application spits out, like uncaught exceptions, so these Azure trace logs are valuable. The name is a bit misleading because there is not a lot of detail in these logs.

How do I run an Azure App service from console?

To begin, go to the Azure Portal and select an App Service that you’ve already created and click on Console under Development Tools. This brings up a command prompt from where you can work with your Azure App Service (see Figure 1 ). As you can see in Figure 1, the prompt starts in D:\home\site\wwwroot.

How to view app service logs in Azure portal?

What I did was just to enable the App Service Logs in the Azure Portal. Then you can use the "Log stream" in the left bar to display the application logs. System.Diagnostics.Trace.TraceInformation ("My message!")

How do I run a performance test on Azure App service?

Log into your Azure account and go to the App Service you created and look under Development Tools. You’ll see Performance Test there. Inside the blade, select New to see the Configure Test option, which you can leave at the default setting of Manual Test or change to Visual Studio Web Test.


3 Answers

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 "Verbose" Level 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

like image 134
benjamingranados Avatar answered Oct 24 '22 08:10

benjamingranados


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>();
            });
    }
like image 38
Rob Kraft Avatar answered Oct 24 '22 09:10

Rob Kraft


You can open Kudu and view Console.WritLine output. Open it from portal or:

https://your_app_name_on_azure.scm.azurewebsites.net/api/logstream

Don't forget to enable logs in azure:

enter image description here

like image 27
Alamakanambra Avatar answered Oct 24 '22 07:10

Alamakanambra