【问题标题】:Why can't i access the RoleEnvironment in Application_Start when using a RoleEntryPoint?为什么在使用 RoleEntryPoint 时无法访问 Application_Start 中的 RoleEnvironment?
【发布时间】:2012-03-29 22:52:39
【问题描述】:

我有一个 Azure WebRole,我正在尝试配置日志记录以使用 DiagnosticMonitor。

根据 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。

如何使 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();
        }

【问题讨论】:

  • 当您尝试访问 Web 应用程序 Application_Start 中的 RoleEnvironment 时会发生什么?
  • 这似乎与stackoverflow.com/a/6202411/348841有关,但有人可以解释一下我应该怎么做吗?
  • 您是否尝试从 Application_Start (重新)配置您的 DiagnosticsMonitor?
  • @SteveWilkes 它会引发错误。我检查了 RoleEnvironment.IsAvailable 并且它是错误的。如果我排除 RoleEntryPoint 这是真的。
  • @JonasStensved 我们在该配置中有很多项目..您的csdef中配置了站点吗?

标签: c# azure azure-diagnostics


【解决方案1】:

我已经解决了。

在清理我的解决方案、重建、重新启动 IIS、关闭 azure 模拟器并重新启动 Visual Studio 之后,它突然开始工作了。

我根本没有更改任何代码。

(我什至在发布之前也做了所有这些事情,但只有当我同时做所有这些事情时才有效)

【讨论】:

    【解决方案2】:

    这绝对是正确的示例代码集。您需要在角色中设置所有这些,而不是在您的 Web 应用程序中。

    注意:由于 Azure 现在拥有完整的 IIS,因此 RoleEntryPoint On_start 和 Web 应用程序之间的上下文不同,后者在 IIS 内自己的工作池中运行。

    只是一个快速的理智清单:

    • 您正在编写的代码在继承自 RoleEntryPoint 的类中(通常是 WebRole.cs,不在 Global.asax 中)?
    • 您正在 Azure 模拟器中运行项目(不是无意中直接启动了 Web 项目?)

    如果您在 Azure 模拟器中运行应用程序或部署到 Azure 本身,只要您有相关的 DLL 引用,就可以在 IIS 应用程序中使用 RoleEnvironment。如果您可以在代码中使用 RoleEnvironment.IsAvailable 进行构建,则包含这些库。我唯一能想到的是,您是在直接运行网站,而不是在 Azure 模拟器中。

    在 Visual Studio 中将 Cloud 项目设置为您的启动项目,您应该是黄金项目。

    【讨论】:

    • 两个问题都是。 WebRole 类继承 RoleEntryPoint,我正在使用模拟器。如果我只是从我的项目中排除 WebRole.cs,我可以再次从应用程序访问 RoleEnvironment。
    猜你喜欢
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 2022-01-04
    相关资源
    最近更新 更多