How to capture Ilogger.LogInformaiton in Azure portal for AppService Logfile implemented in ASP.NET Core 6

113 views Asked by At

With the help of forum and research I am able to view the logfile for the errors occur in hosted ASP.NET Core 6 Web API. So when the error occurred, I captured that under _logger.LogError("error).

But I also implemented _logger.LogInformation("information"). I do want to see this regardless of error. After the implementation, I do not see the information logs.

Please see the below configurations:

Program file:

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseUrls("https://*:5001").
        ConfigureLogging(logging => { logging.AddAzureWebAppDiagnostics(); })
        .ConfigureServices(services=>
        {
            services.Configure<AzureFileLoggerOptions>(options =>
            {
                options.FileName = "my-azure-diagnostics-";
                options.FileSizeLimit = 50 * 1024;
                options.RetainedFileCountLimit = 5;
            });
        })
            .UseStartup<Startup>();

AppSettings.json:

 "Logging": {
  "LogLevel": {
     "Default": "Information",
     "Microsoft": "Warning"
    }
 },
1

There are 1 answers

7
Harshitha On

As you are working with ASP.NET Core 6 we can configure the middleware directly in Program.cs file.

As mentioned earlier in my previous answer, I have used the same code to log traces in a file.

builder.Services.Configure<AzureFileLoggerOptions>(options => 
{
    options.FileName = "LogTracestoFile";
    options.FileSizeLimit = 50 * 1024;
    options.RetainedFileCountLimit = 3;
}); 

My Program.cs file:

builder.Logging.AddAzureWebAppDiagnostics();
builder.Services.Configure<AzureFileLoggerOptions>(options =>
{
    options.FileName = "LogTracestoFile";
    options.FileSizeLimit = 50 * 1024;
    options.RetainedFileCountLimit = 3;
});

My .csproj file:

 <ItemGroup>
   <PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" Version="8.0.0" />
   <PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
 </ItemGroup>
  • To log traces in a file, the app uses the Application Logging settings from App Service logs .

enter image description here

*** Log stream:***

enter image description here

In file (C:\home\LogFiles\Application):

2023-12-05 09:50:46.141 +00:00 [Information] LogsILogger.Controllers.WeatherForecastController: Information Log in Log Stream
2023-12-05 09:50:46.175 +00:00 [Warning] LogsILogger.Controllers.WeatherForecastController: Warning Log
2023-12-05 09:50:46.186 +00:00 [Error] LogsILogger.Controllers.WeatherForecastController: Error Log
2023-12-05 09:50:46.189 +00:00 [Critical] LogsILogger.Controllers.WeatherForecastController: Critical Log