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

nest picture nest · Feb 29, 2016 · Viewed 31.6k times · Source

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?

Answer

Derek picture Derek · Mar 1, 2016

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.