0

i currently have a very basic .NET 5 Blazor Server App running at my azure app service.

Deployment works as expected, app is running and i can call my service properly. Now i tried to add logging to azure stream and it worked sometimes, now not anymore for a not known reason. Following is happening in error state:

I have configured logging within Program.cs in CreateHostBuilder

 public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureLogging(logging => { logging.ClearProviders(); //logging.AddAzureWebAppDiagnostics(); //<== NEVER EVER add this again in .net core as it is implicit called and will cause missing logs!!!: https://wakeupandcode.com/logging-in-asp-net-core/ logging.AddConsole(); logging.AddDebug(); }) .ConfigureServices(serviceCollection => serviceCollection .Configure<AzureFileLoggerOptions>(options => { options.FileName = $"{DateTime.UtcNow.Hour.ToString()}_{Assembly.GetExecutingAssembly().GetName().Name}_diagnostics_"; options.FileSizeLimit = 50 * 1024; options.RetainedFileCountLimit = 5; })) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); 

As you may see i commented AddAzureWebAppDiagnostics out as it is implicit called from framework when deployed to azure, so there is no need to call it explicitly! AND I had this issue short time before, and when i commented it out logging worked fine again, so i thought this was the problem... it was not!

Now problem occured again, so more details:

I use this configured logging first in startup.cs

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger) { _logger = logger; this._logger.LogPrettyInformation($"Current Environment is: {env.EnvironmentName}"); if (env.IsDevelopment() || env.IsStaging()) { string envi = env.IsDevelopment() ? EnvironmentEnum.Development.ToString() : EnvironmentEnum.Staging.ToString(); this._logger.LogPrettyInformation($"Met condition for: {envi}"); app.UseDeveloperExceptionPage(); } else if (env.EnvironmentName.Equals(EnvironmentEnum.Debug.ToString())) { this._logger.LogPrettyInformation($"Met condition for: {EnvironmentEnum.Debug.ToString()}"); app.UseDeveloperExceptionPage(); } else if (env.IsProduction()) { this._logger.LogPrettyInformation($"Met condition for: {EnvironmentEnum.Production.ToString()}"); app.UseExceptionHandler("/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); endpoints.MapBlazorHub(); endpoints.MapFallbackToPage("/_Host"); }); } 

These lines with all that LogPretty-stuff are all working just fine! I can see them at my local debbuging output AND at azure protocol stream too! Looks good ha?

Now I use DI to log from pages and services and stuff, this is the part that is not working any more, but worked from time to time. Now when i call my Counter page as brought up to me by the VS template (WeatherForecastService is the same issue...) I have some logs after i increase the count. But they are only shown in Debug output in my Visual Studio debugging session, NOT in my azure stream.

Any Ideas? In short:

  • all logs working fine in debug session (output window)
  • logs from startup working fine in debug and in azure stream and of course in filesystem under application as configured
  • others not! Not in file and not in stream, just in debug output!

Method for logging - LogPretty - is not throwing any exception.

Thank you guys!

1 Answer 1

2

As per your usage of logging in Startup.cs and program.cs is all good.

logging.AddDebug(); 

It gives the debug logging in local. Refer here for logging .net 5 application

I am following the below steps to get the log stream information

Enable the filesystem logs on Azure Portal

To enable the logs, In Azure portal go to App service -> App Service Logs.

Enable the Application Logging (Filesystem). Then put the Verbose level to be sure to not miss any log. enter image description here

Write the configuration code

Use the package Microsoft.Extensions.Logging.AzureAppServices

Then, add the following code while you configure your web host:

using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.AzureAppServices; using Microsoft.Extensions.DependencyInjection; public class Program { public static void Main(string[] args) => CreateWebHostBuilder(args).Build().Run(); public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .ConfigureLogging(logging => { logging.ClearProviders(); // We have to be precise on the logging levels logging.AddConsole(); logging.AddDebug(); logging.AddAzureWebAppDiagnostics(); // which is not required }) .ConfigureServices(services => { services.Configure<AzureFileLoggerOptions>(options => { options.FileName = "my-azure-diagnostics-"; options.FileSizeLimit = 50 * 1024; options.RetainedFileCountLimit = 5; }); }) .UseStartup<Startup>(); } 

Use below lines of code in a controller or anywhere else, acquire the logger via IoC and start the logging.

private ILogger Logger { get; } public AwesomeController(ILoggerFactory loggerFactory) { this.Logger = loggerFactory.CreateLogger("AwesomeLogger"); } [HttpGet] public void Get() { this.Logger.Debug("DEBUG message. GET enpoint was called."); this.Logger.Error("Error message. Something went wrong !"); } 

Check the logs via Log Stream (In azure portal go to App Service -> Log Stream)

Refer here

Sign up to request clarification or add additional context in comments.

2 Comments

Under which section I can see the same in App Service > Monitoring > Logs?
yes, you can see all type of telemetry information in App Service -> Monitoring -> Logs

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.