Windows Azure Trace Log not working

2019-03-19 00:44发布

问题:

I'm sure I have missed something simple but I can't get simple Trace.WriteLine to work on Azure.

Steps I have taken:

Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString has been set up to our Azure storage account

Import Module Diagnostics to service definition file.

Web config:

  <system.diagnostics>
    <switches>
      <add name="logLevel" value="4" />
    </switches>
    <trace autoflush="false" indentsize="4">
      <listeners>
        <add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
          name="AzureDiagnostics">
        </add>
      </listeners>
    </trace>

  </system.diagnostics>

WebRole.cs

public class WebRole : RoleEntryPoint
{
    public override bool OnStart()
    {


        String wadConnectionString = "Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString";

        CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue(wadConnectionString));

        RoleInstanceDiagnosticManager roleInstanceDiagnosticManager =
                cloudStorageAccount.CreateRoleInstanceDiagnosticManager(
                RoleEnvironment.DeploymentId,
                RoleEnvironment.CurrentRoleInstance.Role.Name,
                RoleEnvironment.CurrentRoleInstance.Id);

        DiagnosticMonitorConfiguration diagnosticMonitorConfiguration =
            roleInstanceDiagnosticManager.GetCurrentConfiguration();

        diagnosticMonitorConfiguration.Directories.ScheduledTransferPeriod =
               TimeSpan.FromMinutes(5d);

        diagnosticMonitorConfiguration.Logs.ScheduledTransferPeriod =
               TimeSpan.FromMinutes(1d);

        diagnosticMonitorConfiguration.Logs.ScheduledTransferLogLevelFilter = LogLevel.Verbose;

        roleInstanceDiagnosticManager.SetCurrentConfiguration
              (diagnosticMonitorConfiguration);

        Trace.WriteLine("This is the message");
        Debug.Write("This is the debug message");
        System.Diagnostics.Trace.TraceError("message2");
        System.Diagnostics.Trace.TraceWarning("message warning");
        System.Diagnostics.Trace.TraceInformation("message warning");
        Trace.Flush();
        return base.OnStart();

    }
}

Solution is compiled as release.

When I view the objects in the storage account I can see a Table called WADDirectoriesTable and three blobs created called vsdeploy, wad-control-container and was-iis-logfiles.

Nothing that looks like my Trace information.

Many thanks

回答1:

I had this same problem. I was using the solution here. I believe the piece you are missing is the location where logs are scheduled to be transferred (in this case, using a LocalResource path):

public override bool OnStart()
{
    Trace.WriteLine("Entering OnStart...");

    var traceResource = RoleEnvironment.GetLocalResource("TraceFiles");
    var config = DiagnosticMonitor.GetDefaultInitialConfiguration();

    // *** this part specifies where transfers should be stored ***
    config.Directories.DataSources.Add(
        new DirectoryConfiguration
        {
            Path = traceResource.RootPath,
            Container = "traces",
            DirectoryQuotaInMB = 100
        });
    config.Directories.ScheduledTransferPeriod = TimeSpan.FromMinutes(10);

    DiagnosticMonitor.Start("DiagnosticsConnectionString", config);

    return base.OnStart();
}  

In order for this to work, you have to create a LocalStorage node to store these files in your ServiceDefinition.csdef:

<LocalStorage name="TraceFiles" sizeInMB="100" cleanOnRoleRecycle="true" />

and you need to have a way to transfer these files to some place where you can get to them, because they are not available in the storage account, but on a local resource folder on the VM itself. I accomplish that with a webpage that allows me to download local resource files.



回答2:

Right, sorted!

This post explains all: http://social.msdn.microsoft.com/Forums/pl-PL/windowsazuredata/thread/d3f2f1d7-f11e-4840-80f7-f61dc11742fb

It's because in Azure SDK 1.3 upwards the listeners are different in the webrole.cs file from the rest of the web app due to it running fully in IIS. If I add trace items to the web app itself the table WADLogsTable appears with my trace information.



回答3:

I had similar problems myself, which the above posts didn't answer for me. I put together a blog post outlining the steps I took to get Diagnostics working with SDK 1.6

Windows Azure Diagnostics with SDK 1.6 for WebRoles