John
John

Reputation: 10969

Azure Logging with WebGet web service

I had an existing WCF web service that contains three WebGet handlers in one .cs file. These handlers make appropriate calls to other .cs files to handle the action.

Now, I created an Azure project based on this web service by creating the Azure project and adding the existing solution. Now, I would like to use Trace.WriteLine for logging. I set up the .cscfg, .csdef, and Web.config files to allow for logging to Azure storage.

However, I don't know where to set up the following info:

//Get the default configuration
DiagnosticMonitorConfiguration dmc = DiagnosticMonitor.GetDefaultInitialConfiguration();

//Set the schedule to transfer logs every 10 mins to the storage                
dmc.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(10);

//Start Diagnostics Monitor with the storage account configuration
DiagnosticMonitor.Start("DiagnosticsConnectionString",dmc);

I tried placing it in a separate internal static class that contains a logger method, but I get a RoleEnvironmentException:error when I run the web service.

Since I do not have an OnStart() method in my web service class with the three WebGets, where do I put it to make sure that I have the diagnostic monitor started correctly, so that I can use Trace.WriteLine?

Upvotes: 0

Views: 683

Answers (3)

Paul
Paul

Reputation: 1947

Late follow up for future viewers... In my case I had enabled Diagnostics in > Roles > . So when I added DiagnosticMonitor.Start to Global.aspx > Application_Start it basically started again. Since I wanted to customize the configuration I just disabled the built-in diagnostics feature in the role's configuration.

Upvotes: 1

knightpfhor
knightpfhor

Reputation: 9399

This configuration code doesn't specifically need to be set on every class that is running code, it simply needs to be run once when your web role starts. The usual place to do this is in the RoleEntryPoint. Because you created this project and then added it to your cloud project, then you won't have a RoleEntryPoint with an OnStart() method that you can override. But that doesn't mean that you can't have one.

You have two options;

  1. Simply add a new class to your web project that inherits from Microsoft.WindowsAzure.ServiceRuntime.RoleEntryPoint and override the OnStart() to setup your diagnostics (this would be my preferred option)
  2. Place the same code somewhere else in your code such that it only runs once like Application_Start in the global.asax

Upvotes: 2

Related Questions