28

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.

0

5 Answers 5

19

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

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

5 Comments

I have followed the exact same settings and have not had any luck!
Thanks, this answer was helpful. Also, it is worth mentioning that you can download App Service log files in the Azure portal using the Kudo console. To open the Kudo console, navigate to your App Service in Azure, the click on Development Tools | Advanced Tools. Once Kudo is open, click on the 'Debug console' menu and select 'CMD'. At that point you should see a folder view at the top of the page allowing you to access/download files and folders (i.e. LogFiles or LogFiles/http).
It is not working for me. Mine is a blazer server app.
Where in the Azure Portal are you finding that "Application logging (Filesystem)" option? As in, what page is it on? UPDATE: I think I found it under the "App Service logs" blade.
That solution doesn't work for me
8

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>();
            });
    }

1 Comment

thank you I have been struggling with this all night, they don't have this section showing up in the tutorial
3

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

Comments

0

Yes, this(console or trace output) is not supported in .NET core only. You should take use of ILogger as per steps below.

In Startup.cs -> Configure method, re-write Configure method like below:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
       //your other code

        //add the following 2 lines of code.
        loggerFactory.AddConsole();
        loggerFactory.AddDebug();


        app.UseStaticFiles();

        //your other code
    }

Then in HomeController.cs(for example), add the following code:

 public class HomeController : Controller
 {
    private readonly ILogger _logger; 

    public HomeController(ILoggerFactory loggerFactory)
    {
        _logger = loggerFactory.CreateLogger<HomeController>();
    }

    public IActionResult Index()
    {
        _logger.LogInformation("this is a information from ILogger...");

        return View();
    }

    //other code

  }

Please let me know if you still have more issues.

1 Comment

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.
0

Try to use the namespace System.Diagnostics.Trace to register info about diagnostic eg: System.Diagnostics.Trace.TraceError("If you're seeing this, something bad happened");

By Default, ASP.NET Core use the provider for logs this namespace: Microsoft.Extensions.Logging.AzureAppServices.

Comments

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.