2012-03-29 33 views
1

我有一個Azure WebRole,我正嘗試使用DiagnosticMonitor配置日誌記錄。爲什麼在使用RoleEntryPoint時無法訪問Application_Start中的RoleEnvironment?

根據在windowsazure.com測井應的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/

爲了訪問OnStart方法我必須定義一個RoleEntryPoint。但一旦定義,我就無法訪問Web應用程序Application_Start中的RoleEnvironment。

如何使角色環境可用於應用程序,同時仍能夠使用DiagnosticMonitor?

我將應用程序連接字符串存儲在服務配置中。

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(); 
     } 
+0

什麼,當你試圖在Web應用程序的Application_Start訪問RoleEnvironment發生什麼呢? – 2012-03-29 12:47:41

+0

這似乎與這個http://stackoverflow.com/a/6202411/348841有什麼關係,但有人可以解釋我應該怎麼做? – 2012-03-29 12:49:39

+0

您是否試圖從Application_Start(重新)配置您的DiagnosticsMonitor? – 2012-03-29 12:49:53

回答

1

我解決了它。

清洗我的解決方案,重建,重新啓動IIS,關停蔚藍的仿真器並重新啓動Visual Studio中後,它突然開始工作。

我根本沒有更改任何代碼。

(我甚至做所有這些事情發佈以及之前但是當我做這一切在同一時間它只是工作)

0

這是defintely正確的示例代碼集。您需要將所有這些設置在角色中,而不是在您的Web應用程序中。

注:由於Azure的現在有完整的IIS的上下文是RoleEntryPoint On_start和Web應用程序,它在它自己的工作池IIS中運行的不同。

只是一個快速的理智清單:

  • 你寫的代碼是在你的類,它從RoleEntryPoint繼承(通常WebRole.cs不是在Global.asax)?
  • 你正在運行在Azure的模擬器項目(不小心直接啓動Web項目?)

如果你正在運行在Azure的模擬器應用程序或部署到Azure的本身RoleEnvironment可從內你的IIS應用程序,只要你有相關的DLL參考。如果您可以在您的代碼中使用RoleEnvironment.IsAvailable構建,則包含這些庫。我唯一能想到的就是你直接運行網站,而不是在Azure模擬器中運行。

將Cloud項目設置爲Visual Studio中的啓動項,您應該是金手指。

+0

對於這兩個問題都可以。類WebRole繼承了RoleEntryPoint,我正在使用模擬器。如果我只從我的項目中排除WebRole.cs,我可以再次從應用程序訪問RoleEnvironment。 – 2012-03-29 13:46:55

相關問題