2

I've instrumented my Azure app service web application (.Net 4.6.1) using custom TraceSources with trace switches so that I can have granular control over my tracing volume at run time. After publishing to my Azure web site, none of the trace statements appear in the streaming logs that are output from Azure. When I put Trace.WriteLine statements in my code I see them appear.

Trace.WriteLine("Great Success");
TraceSources.Session.Warn("Ultimate Failure");

There are a handful of articles on the web dating back to 2012-2014 which reveal that adding an AzureDriveTraceListener listener to my custom sources will cause my data to appear in the streaming logs, but AzureDriveTraceListener doesn't appear to have official support from Microsoft, and the Microsoft.WindowsAzure.WebSites.Diagnostics assembly where it is located is not available from the Azure SDK (currently 2.9) or NuGet (though someone has published an assembly under that name). This guy asked a similar SO question years ago, but it was about the emulator.

Is there an official or approved way to surface TraceSource.TraceEvent or Traceinformation statements in Azure diagnostic logs?

Josh
  • 4,009
  • 2
  • 31
  • 46

1 Answers1

0

Is there an alternative to AzureDriveTraceListener to get TraceSource events to appear in Azure web site streaming logs?

Yes, we could use AzureDriveTraceListener to do that. I do a test demo for it. The following is my detail steps:

1.Create an Asp.net project

2.Reference the Microsoft.WindowsAzure.WebSites.Diagnostics SDK.

3.Add the following config in the web.config file

<system.diagnostics>
      <sharedListeners>
        <add name="AzureDriveTraceListener" type="Microsoft.WindowsAzure.WebSites.Diagnostics.AzureDriveTraceListener, Microsoft.WindowsAzure.WebSites.Diagnostics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
      </sharedListeners>
      <sources>
        <source name="TraceSourceLogging" switchName="TestSwitch" switchType="System.Diagnostics.SourceSwitch">
          <listeners>
            <add name="AzureDriveTraceListener"/>
          </listeners>
        </source>
      </sources>
      <switches>
        <add name="TestSwitch" value="Verbose" />
      </switches>
    </system.diagnostics>

4.Add an AppTrace class in the project, we could get the demo code from the blog

 public static class AppTrace
    {
        public static TraceSource TraceSource { get; set; }

        static AppTrace()
        {
            TraceSource = new TraceSource("TraceSourceLogging")
            {
                Switch = {Level = (SourceLevels) Enum.Parse(typeof(SourceLevels), "All", true)}
            };
        }

        private static string Format(string message, string memberName, string filePath, int lineNumber)
        {
            return $"Message: {message}, MemberName: {memberName}, FilePath: {filePath}, LineNumber: {lineNumber}";
        }
        public static void Verbose(string message, int id = 16, [CallerMemberName] string memberName = "",
            [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
        {
            TraceSource.TraceEvent(TraceEventType.Verbose, id, Format(message, memberName, filePath, lineNumber));
        }

        public static void Error(string message, int id = 2, [CallerMemberName] string memberName = "",
            [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
        {
            TraceSource.TraceEvent(TraceEventType.Error, id, Format(message, memberName, filePath, lineNumber));
        }

        public static void Information(string message, int id = 8, [CallerMemberName] string memberName = "",
            [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
        {

            TraceSource.TraceEvent(TraceEventType.Information, id, Format(message, memberName, filePath, lineNumber));
        }
        public static void Critical(string message, int id = 1, [CallerMemberName]string memberName =
            "", [CallerFilePath] string filePath = "", [CallerLineNumber]int lineNumber = 0)
        {
            TraceSource.TraceEvent(TraceEventType.Critical, id, Format(message, memberName, filePath, lineNumber));
        }

        public static void Warning(string message, int id = 4, [CallerMemberName]string memberName =
            "", [CallerFilePath] string filePath = "", [CallerLineNumber]int lineNumber = 0)
        {
            TraceSource.TraceEvent(TraceEventType.Warning, id, Format(message, memberName, filePath, lineNumber));
        }

        public static void Start(string service, int id = 256, [CallerMemberName]string memberName =
            "", [CallerFilePath] string filePath = "", [CallerLineNumber]int lineNumber = 0)
        {
            TraceSource.TraceEvent(TraceEventType.Start, id, Format("Starting - " + service, memberName, filePath, lineNumber));
        }

        public static void Stop(string service, int id = 512, [CallerMemberName]string memberName =
            "", [CallerFilePath] string filePath = "", [CallerLineNumber]int lineNumber = 0)
        {
            TraceSource.TraceEvent(TraceEventType.Stop, id, Format("Stoping - " + service, memberName, filePath, lineNumber));
        }
    } 

5.Add a test page.

protected void Page_Load(object sender, EventArgs e)
    {

        AppTrace.Verbose("Test Verbose");
        AppTrace.Error("Test Error");
        AppTrace.Warning("Test Warning");
        AppTrace.Information("Test Information");
        AppTrace.Critical("Test Critical");

        Label1.Text = "Completed";

    }

6.Publish the project to Azure.

7.Visit the page and check it from Azure portal

enter image description here

Tom Sun - MSFT
  • 24,161
  • 3
  • 30
  • 47
  • Thank you Tom Sun. I know it works, but Microsoft don't have any official documentation for AzureDriveTraceListener that I can find, so I wonder if it's safe to use that undocumented feature. It's also not usable for local debugging, so it's only an option to add this in a release transform. This question has been asked in one form or another for 5 years and would have expected to see something official from MS about how to accomplish this. – Josh Jul 19 '17 at 13:07
  • Also, the NuGet package you linked to is in the Microsoft namespace, but it's not a Microsoft release. I personally never download anything from Nuget that claims to be a Microsoft package but is not published by an official Microsof source – Josh Jul 19 '17 at 14:07
  • From the [nuget License link](https://raw.githubusercontent.com/Microsoft/dotnet/master/LICENSE), we could know that copy right of sdk is Microsoft, so you can easy-to-use. – Tom Sun - MSFT Jul 20 '17 at 02:21
  • If you want to local debug, it seems that we could use `System.Diagnostics.ConsoleTraceListener` to do that. – Tom Sun - MSFT Jul 20 '17 at 02:23