Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging from ASP.NET 5 application hosted as Azure Web App

Tags:

I have an ASP.NET 5 Web API that I host in Azure as a Web App. I want to log messages from my code using Azure Diagnostics. There are multiple article including Azure docs that suggest that it should be as easy as System.Diagnostics.Trace.WriteLine once enabled. The logs should show up under LogsFiles/Application and in log stream in Azure.

I enabled application logging for the web app:

enter image description here

But the following calls produces no logs:

System.Diagnostics.Trace.TraceError("TEST");
System.Diagnostics.Trace.TraceInformation("TEST");
System.Diagnostics.Trace.TraceWarning("TEST");
System.Diagnostics.Trace.WriteLine("TEST");

I tried to manually define TRACE symbol, but with no luck:

enter image description here

I also tried to use the new Microsoft.Extensions.Logging framework and use ILogger.Log API, but without any results again:

public void Configure(IApplicationBuilder app, 
                      IHostingEnvironment env, 
                      ILoggerFactory loggerFactory)
{
    loggerFactory.MinimumLevel = LogLevel.Debug;

    var sourceSwitch = new SourceSwitch("Sandbox.AspNet5.ApiApp-Demo");
    sourceSwitch.Level = SourceLevels.All;
    loggerFactory.AddTraceSource(sourceSwitch, 
                                 new ConsoleTraceListener(false));
    loggerFactory.AddTraceSource(sourceSwitch, 
                                 new EventLogTraceListener("Application"));
}

Any ideas on what am I doing wrong?

like image 481
Nikolai Samteladze Avatar asked Jan 19 '16 00:01

Nikolai Samteladze


People also ask

Is .NET 5 supported in Azure?

As of March 2021, Microsoft announced that Azure Functions are supported running on . NET 5.

How do I enable application logs in Azure app?

To enable application logging for Windows apps in the Azure portal, navigate to your app and select App Service logs. Select On for either Application Logging (Filesystem) or Application Logging (Blob), or both. The Filesystem option is for temporary debugging purposes, and turns itself off in 12 hours.

How do I log into Azure Web App?

That's pretty much it. Now you can simply visit your application using the URL you can see on your Azure dashboard. On the portal, click on “Log Stream” immediately below “App Services log” and you'll see the log messages in real time.


2 Answers

If you look at your web.config, it probably has stdoutLogEnabled="false". If you set that to true, then anything that is written to standard output will be written to a file. And the stdoutLogFile determines where it goes, which by default is under d:\home\logfiles.

Now you need to make sure that your logging actually goes to stdout. Doing Console.WriteLine would definitely work. I think it's probably possible to also configure things via ILoggerFactory in your startup.cs to make logs go to stdout.

like image 61
David Ebbo Avatar answered Sep 30 '22 16:09

David Ebbo


I've found an simple trick for azure app ,see https://github.com/aspnet/Logging/tree/dev/src/Microsoft.Extensions.Logging.AzureAppServices, please add the package "Microsoft.Extensions.Logging.AzureAppServices": "1.0.0-preview1-final" and update related dependencies, add the azure diagnostics in startup.cs like this :

loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
loggerFactory.AddAzureWebAppDiagnostics(); // for default setting.

or for custom setting:

loggerFactory.AddAzureWebAppDiagnostics(new AzureAppServicesDiagnosticsSettings( ...)); // add custom setting.
// see here for detailed member properties: https://github.com/aspnet/Logging/blob/dev/src/Microsoft.Extensions.Logging.AzureAppServices/AzureAppServicesDiagnosticsSettings.cs

And enable the diagnostics log on azure, both logging on blob and file are work well. No need for any extra configuration. :)

like image 27
Sharpeli Avatar answered Sep 30 '22 17:09

Sharpeli