I thought this would be simple but after several hours have realized that it is not. I deployed an "App Service" to Azure that is a C# .Net Core.
I am trying to add some crude monitoring of by app by using Console.WriteLine("my message") or Trace.TraceError("my message") but I can't find that output anywhere in Azure.
I tried enabling Application Insights but didn't find the output there either.
I just want the simplest way to get a little idea that a line of code in my app is getting hit. What is that simplest way?
Things I have tried: 1) I went in to the console in Azure and browsed to every file that might have this sort of output but did not find any. Certainly nothing in any of the files and folders under LogFiles. 2) I DID go to "App Service Logs" and enabled "Application Logging (Filesystem)". 3) I also enabled "Detailed error messages" in "App Service Logs". 4) I tried "Diagnostic settings (preview)" but could not find the output there. 5) I watch the "Log Stream" for Application Logs but nothing shows up there 6) Under "Logs" I just have this message: "We didn’t find any logs" 7) Neither "metrics" nor "Alerts" has this log output
I am beginning to suspect this is not support in Azure. Do I need to add a logging framework like serilog just to one time add a statement to may app like "You hit line 20"? I really just want something quick and dirty, but after spending a few hours on this the "quick" part of it isn't happening.
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.
In Solution Explorer, right-click the project, and click Publish. In the Profile drop-down list, select the same profile that you used in Create an ASP.NET app in Azure App Service. Then, click Settings. In the Publish dialog, click the Settings tab, and then change Configuration to Debug, and then click Save.
View logs in Application InsightsGo to Application Insights resource in your resource group. Go to Logs under Monitoring section. Click on traces eye button to get log traces. Select Time Range and click Run.
These logs come in .txt format and also contain any generic information that your application spits out, like uncaught exceptions, so these Azure trace logs are valuable. The name is a bit misleading because there is not a lot of detail in these logs.
To begin, go to the Azure Portal and select an App Service that you’ve already created and click on Console under Development Tools. This brings up a command prompt from where you can work with your Azure App Service (see Figure 1 ). As you can see in Figure 1, the prompt starts in D:\home\site\wwwroot.
What I did was just to enable the App Service Logs in the Azure Portal. Then you can use the "Log stream" in the left bar to display the application logs. System.Diagnostics.Trace.TraceInformation ("My message!")
Log into your Azure account and go to the App Service you created and look under Development Tools. You’ll see Performance Test there. Inside the blade, select New to see the Configure Test option, which you can leave at the default setting of Manual Test or change to Visual Studio Web Test.
What I did was just to enable the App Service Logs in the Azure Portal.
Specifically I turned on the "Application Logging (Filesystem)" at the "Verbose" Level and then I selected "File System" in the "Web server logging" option
Then you can use the "Log stream" in the left bar to display the application logs.
In your code you just have to call
System.Diagnostics.Trace.TraceInformation("My message!")
Or
System.Diagnostics.Trace.WriteLine("My message!")
And that's it!
Here I include some screenshots:
I finally figured it out myself. I needed to add configuring AzureFileLoggerOptions. Without those options, nothing showed up in the Azure output.
.ConfigureLogging(logging => logging.AddAzureWebAppDiagnostics())
.ConfigureServices(serviceCollection => serviceCollection
.Configure<AzureFileLoggerOptions>(options =>
{
options.FileName = "azure-diagnostics-";
options.FileSizeLimit = 50 * 1024;
options.RetainedFileCountLimit = 5;
}).Configure<AzureBlobLoggerOptions>(options =>
{
options.BlobName = "log.txt";
})
)
In summary, for .Net Core 3.1, to get your messages to show up in the "Log Stream", you need to enable "Application Logging" in the "App Service Logs" section of Azure. In your code you need to reference:
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.AzureAppServices;
Also, in my case, I am using Blazor/SignalR and don't have an MVC Controller, so I couldn't figure out how to access the Logging framework in my class. I am sure there is a better way, but by exposing the ILogger as in the code below I was able to reference it from anywhere within my code base.
I have a static method I call to write to the log (console when I am actively debugging on my pc, or Azure "Log Stream" when running in Azure:
public static void WriteToLog(string message)
{
Program.Logger.LogError(message);
}
Code in my Program.cs looks like this:
public class Program
{
public static ILogger Logger;
public static void Main(string[] args)
{
//CreateHostBuilder(args).Build().Run();
var host = CreateHostBuilder(args).Build();
var LoggerF = LoggerFactory.Create(builder =>
{
builder.AddFilter("BlazorDiceRoller", LogLevel.Warning)
.AddConsole().AddAzureWebAppDiagnostics()
.AddFilter("Microsoft", LogLevel.Warning)
.AddFilter("System", LogLevel.Warning);
});
Logger = LoggerF.CreateLogger<Program>();
host.Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging(logging => logging.AddAzureWebAppDiagnostics())
.ConfigureServices(serviceCollection => serviceCollection
.Configure<AzureFileLoggerOptions>(options =>
{
options.FileName = "azure-diagnostics-";
options.FileSizeLimit = 50 * 1024;
options.RetainedFileCountLimit = 5;
}).Configure<AzureBlobLoggerOptions>(options =>
{
options.BlobName = "log.txt";
})
)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
You can open Kudu and view Console.WritLine
output. Open it from portal or:
https://your_app_name_on_azure.scm.azurewebsites.net/api/logstream
Don't forget to enable logs in azure:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With