Share via


System.Diagnostics.Trace Application Logging Log Stream on Azure App Service Function App

Here is what I wanted to do.

Add some code like this to my ASP.NET application.

 
using System.Diagnostics;
...
System.Diagnostics.Trace.WriteLine("System.Diagnostics.Trace.WriteLine() in the Page_Load method");
Trace.Write("Trace.Write() in the Page_Load method");
System.Diagnostics.Trace.TraceError("System.Diagnostics.Trace.TraceError() in the Page_Load method");
System.Diagnostics.Trace.TraceWarning("System.Diagnostics.Trace.TraceWarning() in the Page_Load method");
System.Diagnostics.Trace
      .TraceInformation("System.Diagnostics.Trace.TraceInformation() in the Page_Load method");

Then I wanted to write those logs out to the Application Logging (File System), Figure 1, Application Logging (Blob), Figure 2 and to the Log stream window in the portal, Figure 3.

image

Figure 1, how to write System.Diagnostics.Trace logs to application logging (file system) image

Figure 2, how to write System.Diagnostics.Trace logs to application logging (blob) image

Figure 3, how to write System.Diagnostics.Trace logs to application Log stream

Obviously, you can see that I did it.  But how Benjamin, HOW!?  Well, first enable it, as seen in Figure 4.

image

Figure 4, how to enable Application Logging to log System.Diagnostics.Trace

Then, the way I got this to work was to add the following to the top of the page which I wanted to log the traces from, Figure 5.

 
#define TRACE

image

Figure 5, how to enable Application Logging to log System.Diagnostics.Trace

For other application types you may be required to enable the tracing in a different way.  If you have some examples please add a comment below.  However, the point I learned is that more than simply adding the System.Diagnostics.Trace() method wasn’t enough to get the logs to be output.

Comments

  • Anonymous
    December 04, 2018
    I'm trying to have exactly the same behavior (Just store in Blob Storage actually), but without success when using a ASP.NET Core deployed as a AppService.It work's properly when using the built-in Logger, but for a specific reason I need to use the System.Diagnostics.Trace.