sebhaub
sebhaub

Reputation: 734

Logging only works localy, but not when deployed to Microsoft Azure App Service

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:

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.

Upvotes: 3

Views: 2020

Answers (1)

Joey Cai
Joey Cai

Reputation: 20127

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.

Upvotes: 3

Related Questions