Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET web application in Azure - How to log errors?

Tags:

I have a web application deployed to azure but I don't know how to log errors.

For testing purposes I have this ForceError method:

public string ForceError() {     throw new Exception("just a test exception");     return "ok"; } 

Which causes and error like this: enter image description here

On Azure I enabled all Diagnostic logs like this: enter image description here

But the error I forced does not appear in selected storage container.

Do you know what should I do to start logging all the errors from the application?

like image 992
nest Avatar asked Feb 29 '16 14:02

nest


People also ask

How do I log errors in Azure?

Log detailed errors To save the error page or failed request tracing for Windows apps in the Azure portal, navigate to your app and select App Service logs. Under Detailed Error Logging or Failed Request Tracing, select On, then select Save.

How do I check Azure App Service errors?

Application Event Log (Azure App Service) To access the Application Event Log, use the Diagnose and solve problems blade in the Azure portal: In the Azure portal, open the app in App Services. Select Diagnose and solve problems.

How do I check Azure webapp logs?

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.

How does ASP.NET handle application level errors?

Application Level Error Handling You can handle default errors at the application level either by modifying your application's configuration or by adding an Application_Error handler in the Global. asax file of your application. You can handle default errors and HTTP errors by adding a customErrors section to the Web.


2 Answers

I am afraid just throwing an exception doesn't work in Azure Web application logging.

ASP.NET applications can use the System.Diagnostics.Trace class to log information to the application diagnostics log. The four methods in example below correspond with the diagnostic log levels:

Trace.TraceError("Message"); // Write an error message    Trace.TraceWarning("Message"); // Write a warning message Trace.TraceInformation("Message"); // Write an information message Trace.WriteLine("Message"); // Write a verbose message 

enter image description here

Besides the basic information for logged events, blob storage log additional information such as the instance ID, thread ID, and a more granular timestamp (tick format) in CSV.

enter image description here

A great article here about logging tips and tools.

See also the Reference to the official Azure Web Apps Logging Document.

like image 82
Derek Avatar answered Sep 30 '22 11:09

Derek


On Azure Websites, best way to log would be Application Insights, you can use free version to get insights about crashes/speed/performance.

However, Application Insights is little slower if you enable everything. But if you customize it and enable only error logging, it would push all logs to your azure application insights account and you will be able to monitor/analyze it very nicely.

For more details: https://azure.microsoft.com/en-in/documentation/articles/app-insights-api-custom-events-metrics/

Instead of automatically configuring Application Insights, I would suggest, take an empty project, setup application insights. Notice all added config files and nuget packages. There is some insight config file, except application key/signature, you can turn off everything.

Only when you want to track an exception manually, you can create TelemetryClient and call TrackException method. You can pass more details if you need.

like image 37
Akash Kava Avatar answered Sep 30 '22 11:09

Akash Kava