-->

.NET TraceSource not working under Windows Azure

2019-03-30 15:29发布

问题:

I'm trying to display some TraceSource logging information to the Azure emulator (consoley) window.

None of the TraceSource lines are displayed. Only the stock Trace lines and various low level azure messages.

Here's my steps to repo, including code snippets:

  1. File -> New -> Cloud Service (SDK 2.0) -> (add worker role).
  2. Add a TraceSource to the WorkerRole.
  3. Update the app.config with tracing data.
  4. Play/Publish.

NOTE all the other default code is in there, such as the .csfg which says to use UseDevelopmentStorage=true etc

Worker Role code.

This is the STOCK DEFAULT CODE with my TraceSource coded added...

using System.Diagnostics;
using System.Net;
using System.Threading;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace WorkerRole1
{
    public class WorkerRole : RoleEntryPoint
    {
        private TraceSource _traceSource;

        public override void Run()
        {
            _traceSource.TraceEvent(TraceEventType.Verbose, 0,
                                    "********************** 111111111111111111111 ******************* ");

            // This is a sample worker implementation. Replace with your logic.
            Trace.TraceInformation("WorkerRole1 entry point called", "Information");

            while (true)
            {
                _traceSource.TraceEvent(TraceEventType.Verbose, 0,
                                        "********************** 222222222222222222222 ******************* ");
                Thread.Sleep(10000);
                Trace.TraceInformation("Working", "Information");
            }
        }

        public override bool OnStart()
        {
            // Set the maximum number of concurrent connections 
            ServicePointManager.DefaultConnectionLimit = 12;

            // For information on handling configuration changes
            // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.


            _traceSource = new TraceSource("Azure.WorkerRole", SourceLevels.All);

            return base.OnStart();
        }
    }
}

Now the app.config...

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

    <system.diagnostics>

        <sharedListeners>
            <add name="AzureListener"
                 type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
                <filter type="" />
            </add>
        </sharedListeners>

        <sources>
            <source name="Azure.WorkerRole" switchValue="Verbose" >
                <listeners>
                    <add name="AzureListener" />
                </listeners>
            </source>
        </sources>

    </system.diagnostics>
</configuration>

That's it! run and see that the traceSource stuff isn't getting displayed :( The Trace.Information stuff is .. but I don't want to use the old Trace method because that is suggested to be replaced by using TraceSource instead.

Sample output. Notice only the Trace lines are getting added (along with the low level azure stuff).