Search code examples
azureazure-worker-rolesazure-diagnostics

Trace.WriteLine() Messages only Persisted in OnStart() for an Azure WorkerRole


I'm developing an Azure WorkerRole(). In the Compute Emulator console, I see all Trace.WriteLine() messages, but only those generated in OnStart() seem to be persisted into storage.

My ServiceConfiguration.Local.csfg has:

<Role name="MyWorkerRole">
  <Instances count="1" />
  <ConfigurationSettings>
    <Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value="UseDevelopmentStorage=true" />
  </ConfigurationSettings>
</Role>

My WorkerRole.cs has:

public override void Run() {
    Trace.WriteLine("Called from Run(), where does this trace go???", "Information");

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

    DiagnosticMonitorConfiguration dmc = DiagnosticMonitor.GetDefaultInitialConfiguration();
    dmc.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);
    dmc.Logs.ScheduledTransferLogLevelFilter = LogLevel.Verbose;
    DiagnosticMonitor.Start("DiagnosticsConnectionString", dmc);

    Trace.WriteLine("This trace appears in WADLogsTable", "Information");

    return base.OnStart();
}

Solution

  • That looks correct - I wonder why you don't see anything. I wonder if Start is ignoring your config changes somehow. Here is some code I have used that will transfer the trace logs (I know this works):

        private static void EnableDiagnostics(int transferTime)
        {
            string wadConnectionString = "Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString";
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue(wadConnectionString));
    
            RoleInstanceDiagnosticManager roleInstanceDiagnosticManager = storageAccount.CreateRoleInstanceDiagnosticManager(RoleEnvironment.DeploymentId, RoleEnvironment.CurrentRoleInstance.Role.Name, RoleEnvironment.CurrentRoleInstance.Id);
            DiagnosticMonitorConfiguration config = roleInstanceDiagnosticManager.GetCurrentConfiguration();
    
            if (config == null)
            {
                config = DiagnosticMonitor.GetDefaultInitialConfiguration();
            }
    
            config.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(transferTime);
            config.Logs.ScheduledTransferLogLevelFilter = LogLevel.Undefined;
    
            CrashDumps.EnableCollection(true);
    
            roleInstanceDiagnosticManager.SetCurrentConfiguration(config);
        }