目前我希望将我的日志文件删除到特定位置。有没有办法可以在 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/