Jonas Stensved
Jonas Stensved

Reputation: 15326

Why can't i access the RoleEnvironment in Application_Start when using a RoleEntryPoint?

I have a Azure WebRole which I'm trying to configure logging for using the DiagnosticMonitor.

According to the documentation at windowsazure.com the logging should be implemented in OnStart:

Note: The code in the following steps is typically added to the OnStart method of the role.

https://www.windowsazure.com/en-us/develop/net/common-tasks/diagnostics/

In order to access the OnStart method I have to define a RoleEntryPoint. But once it is defined I can't access the RoleEnvironment in web applications Application_Start.

How can I make the RoleEnvironment available to the application while still being able to use the DiagnosticMonitor?

I store the applications connection-strings in the service configuration.

public class WebRole : RoleEntryPoint
    {

        public override bool OnStart()
        {


            // config
            var config = DiagnosticMonitor.GetDefaultInitialConfiguration();

            LocalResource localResource = RoleEnvironment.GetLocalResource("MyCustomLogs");

            DirectoryConfiguration dirConfig = new DirectoryConfiguration();
            dirConfig.Container = "wad-mycustomlogs-container";
            dirConfig.DirectoryQuotaInMB = localResource.MaximumSizeInMegabytes;
            dirConfig.Path = localResource.RootPath;


            DiagnosticMonitorConfiguration diagMonitorConfig = DiagnosticMonitor.GetDefaultInitialConfiguration();
            diagMonitorConfig.Directories.ScheduledTransferPeriod = TimeSpan.FromMinutes(1.0);
            diagMonitorConfig.Directories.DataSources.Add(dirConfig);

            DiagnosticMonitor.Start("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString", config);



            return base.OnStart();
        }

Upvotes: 1

Views: 1859

Answers (2)

Jonas Stensved
Jonas Stensved

Reputation: 15326

I've solved it.

After cleaning my solution, rebuilding, restarting IIS, shutting down the azure emulators and restarting Visual Studio it suddenly started working.

I changed no code at all.

(I even did all of those things before posting as well but it only worked when I did it all at the same time)

Upvotes: 1

Matt Quinn
Matt Quinn

Reputation: 131

This is defintely the right example set of code. You need to set all of this in the Role though NOT in your web application.

NOTE: since Azure now has full IIS the context is different between the RoleEntryPoint On_start and the Web application, which is running in it's own worker pool within IIS.

Just a quick sanity checklist:

  • The code you're writing is in your class that inherits from RoleEntryPoint (typically WebRole.cs NOT in the Global.asax)?
  • You're running the project in the Azure Emulator (not inadvertently starting a web project directly?)

If you're running the application in the Azure emulator or deployed to Azure itself RoleEnvironment is available from within your IIS application as long as you have the relevant DLLs reference. If you can build with RoleEnvironment.IsAvailable in your code, then the libraries are included. The only thing I can think is that you're running the web site directly, not within the Azure emulator.

Set the Cloud project to be your startup in Visual Studio and you should be golden.

Upvotes: 0

Related Questions