VSO
VSO

Reputation: 12656

What to See Trace Logs for Azure App Service?

Using .NET Core 2 in Azure App Service. Don't see my logs. In the actual application, the logging code looks like this:

using System.Diagnostics;
...
Trace.WriteLine("Logging works");

I expected to see the Trace logs in the Log Stream, but I don't. I do see general API logs. What am I doing wrong? My config below:

p

Upvotes: 0

Views: 2889

Answers (1)

Ivan Glasenberg
Ivan Glasenberg

Reputation: 30025

I had the same issue for .NET core azure web app: Trace.WriteLine method does not work(not write message to Application logs) for .NET core, but work for .NET framework , and I find the issue about that.

As a workaround for .NET core web application, I suggest you can use ILogger, which can write message to Application Logs.

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, 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

  }

After publish to azure, and configure the Application Logging -> run the azure web app -> you can see the message is displayed in Application Logs:

enter image description here

Upvotes: 2

Related Questions