c# - 如何设置环境变量并在Azure 2.5的IIS应用程序中使用它们

标签 c# asp.net azure iis

目前我希望将我的日志文件删除到特定位置。有没有办法可以在 Global.asax.cs 中获取 IIS 应用程序的环境变量

private string GetLoggingPath()
        {
            string rootFolder = string.Empty;
            var directoryInfo = new DirectoryInfo(HttpContext.Current.Server.MapPath("~/")).Parent;
            if (directoryInfo != null)
            {
                if (directoryInfo.Parent != null)
                {
                    rootFolder = directoryInfo.Parent.FullName;
                }
            }

            if (string.IsNullOrWhiteSpace(rootFolder))
            {
                return Path.GetTempPath();
            }

            return Path.Combine(rootFolder, @"approot\bin\Data");
        }

我正在研究 RoleRoot 环境变量,但即使应用程序在 Azure 上运行,我也无法使用它。

如何在 azure 中设置环境变量以便可以在 IIS 应用程序中使用?对我来说,无需硬编码即可访问文件夹 E:\approot\bin\data 的最佳方式是什么,其中 E:\ 是 roleroot 文件夹

最佳答案

没有什么好办法,w3wp.exe进程中似乎没有设置环境变量,但是使用以下方法怎么样:

System.IO.Path.Combine(
    System.IO.Path.GetPathRoot(
        System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath
    ),
    @"approot\bin\Data"
); 

(与您几乎相同,但没有创建 DirectoryInfo)。

使用 Process Explorer 查看进程,我可以看到只有 IISConfigurator.exe 具有 RoleRoot 环境变量,因此一种选择是您可以使用 AppCmd.exe 在启动任务期间将其设置到 web.config 中的某个配置文件中或 applicationHost.config,然后从那里读取它。

但说实话,我只会使用上面的代码。

关于c# - 如何设置环境变量并在Azure 2.5的IIS应用程序中使用它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32620226/

相关文章:

c# - Web API 路由无法识别参数

c# - 取消任务会引发异常

c# - 从 .Contains() 语句中仅获取整个单词

asp.net - 我们如何在共享工作环境中使用版本控制?

Azure API 管理配置外部缓存策略

azure - 我在 azure devops 中连接 AAD 时遇到错误

c# - 如果文件名包含特定数字,我正在尝试从文件夹加载所有图像

asp.net - IIS 使用代理进行 Web 服务请求。如何阻止这个?

jquery - 如何创建 jQuery 函数来验证 ASP 复选框列表上的自定义验证器

git - 无法使用 git 存储库创建网站并从 Team Foundation 服务发布