Not able to view the logs for hosted WebApp in Azure

47 Views Asked by At

I have a ASP.NET Core MVC app in Azure. I have implemented the logs in the MVC where I want to capure informational logs and error log. So when needed developer can view the logs in Azure portal. In order to do so, I followed some tutorial and got help from stackoverflow forum and tried different ways and multiple times but I am not able to see the error log in filesystem. My goal is the view the file for information log and error using file system. As there are many ways to view the error but I am not want to just see the error but the information as well and that I want to see in the file. Please see below images for the code and setting in Azure.

--Appsetting.json enter image description here

--Code to capture the error. This method call another method whichly surely thows an error. I used diagonostic jsut to try if that can capture informaiton in log file. However, I have other code that does have code for _loggger.logInformation("Information goes here")

enter image description here

--Turned on the FileSystem loggin in Azure after the deployment was completed. enter image description here

--Kudu view for the file under home>Logfiles>Application. Beside the view, I checked for the files in other folders just incase if file was created at other location but no file was created.

enter image description here

So please help.

1

There are 1 best solutions below

2
Harshitha On

I am able to log the Information and Error messages with the same code which you have shared.

  • Instead of (ex.Message,"Text") , set the Error message as (ex,"Text").
  • You can remove builder.Logging.AddApplicationInsights(); line of code if you don't want to check the logs in Application Insights.

My Controller.cs:

 [HttpGet(Name = "GetWeatherForecast")]
 public IEnumerable<WeatherForecast> Get()
 {
     try
     {
         _logger.LogInformation("Log Information from API");
         throw new Exception();
     }
     catch (Exception ex)
     {
         _logger.LogError(ex.Message, "Errorrrrrrrrrrrr");
         _logger.LogError(ex.StackTrace);
         _logger.LogError("DEBUG: A NEW EXCEPTION without Ex");
         _logger.LogError(ex, "DEBUG ERROR: Error EXCEPTION with ex");
     }
     -----
 }

My appsettings.json file:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  }
}
  • After deploying the App to Azure App Service, In App Service logs enable the Application logging,Web server logging,Detailed error messages and Failed request tracing as shown below.

enter image description here

  • Execute the request from the Controller.

  • Now check for the logs and traces in LogStream.

enter image description here

Kudu view for the file under home>Logfiles>Application

  • Log file with name Diagnostics-Logs(which I have set in Program.cs file) will be created under C:\home\LogFiles\Application folder.

enter image description here

  • The logs which you have seen in LogStream can be seen here as well.

enter image description here