2
votes

i am facing a strange problem while developing an asp.net core application which is deployed to Microsoft Azure and runs there as an app-service. I target the full .net framework. I am using Microsoft.Extensions.Logging and the logs are working locally without a problem, but when deploying it to Microsoft Azure there are no logs at all. The LogStream windows stays empty and just shows the No new Trace in the last n minutes output.

Things I already checked:

  • Enabled Diagnostic Logs in the App-Service Settings
  • Set the LogLevel there to Verbose
  • Checked the LogFiles directory on the App-Service

Part of my appsettings.json

"Logging": {
 "LogLevel": {
   "Default": "Information"
}

I am then injecting the Logger instances to the controllers like this:

ILogger<FooBarClassName> myLogger;

public FooBarClassName(ILogger<FooBarClassName> logger)
{
        myLogger = logger;
        ....
}

And use it like this:

void SomeMethod()
{
    ...
    myLogger.LogInformation("Some Log Message");
    ...
}    

Thanks for any help to solve this issue.

1
What is the logging folder on the Azure App Service? If I remember correctly, you should have writing permissions on D:\Home folder. Also, check stackoverflow.com/questions/48947739/… - Rui Jarimba

1 Answers

3
votes

You could refer to the following steps to check whether you add ILogger correctly.

Step 1:

Go to Startup.cs, inside Configure method add following signature.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)

The ILoggerFactory is default injected into the class by asp.net core.

Setp 2:

Set up provider.

loggerFactory.AddDebug();
loggerFactory.AddAzureWebAppDiagnostics();

The AddAzureWebAppDiagnostics comes from the package.

Setp 3:

Example in my HomeController and the appsettings.json is same with you:

private readonly ILogger _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }
        void SomeMethod()
        {
            _logger.LogInformation("some log message.");
        }
        public IActionResult Index()
        {
            SomeMethod();
            return View();
        }

Step 4:

Set diagnostic logs to turn on applicaiton logging

enter image description here

Step 5:

See logs in Log Stream

enter image description here

For more details, you could refer to this case and this article.