c# - 为什么使用 RoleEntryPoint 时无法访问 Application_Start 中的 RoleEnvironment?

标签 c# azure azure-diagnostics

我有一个 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();
        }

最佳答案

我已经解决了。

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

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

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

关于c# - 为什么使用 RoleEntryPoint 时无法访问 Application_Start 中的 RoleEnvironment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9925234/

相关文章:

azure - 在不使用 .NET 的情况下在 Windows Azure 中启用诊断

azure - 使用arm模板在Azure数据工厂中启用诊断

c# - 使用 Linq 在 JArray 中使用 JSON.NET 多个 orderby

c# - Vim 用于 java 和 C#?

c# - 不使用 Visual Studio 创建 Wix Bootstrapper

python - 如何排查 Azure ML 服务部署问题?

php - 如何在通知中心向特定用户发送推送通知?

c# - 将自签名根证书添加到 Azure 应用服务

azure - 来自 Azure Web App 的一个实例的 HTTP 502

c# - 如何从单元测试中获取项目文件夹中的路径