如何配置我的ASP.NET目录以允许编写日志文件?

mea*_*nny 7 c# asp.net iis

好的,我相信我在这里做的很容易.我编写了一个ASP.NET网页,它只是试图写入本地目录.

我使用以下代码:

System.IO.File.WriteAllLines("log.txt", messages);
Run Code Online (Sandbox Code Playgroud)

我抛出以下异常.

Access to the path 'c:\windows\system32\inetsrv\log.txt' is denied.
Run Code Online (Sandbox Code Playgroud)

我的ASP.NET应用程序位于以下目录中.

c:\inetpub\wwwroot\sites\mysite\
Run Code Online (Sandbox Code Playgroud)

所以当我不提供该目录时,我很困惑为什么它试图写入c:\ windows\system32\inetsrv \目录.

我已经尝试将代码更改为以下,但它给了我与新目录相同的错误消息.

System.IO.File.WriteAllLines("c:\\inetpub\\wwwroot\\sites\mysite\log.txt", messages);
Run Code Online (Sandbox Code Playgroud)

编辑1

很难接受这方面的答案,因为每个人都真的帮了我一大堆.我接受了tom_yes_tom的答案,因为他是第一个发布他的回答的人,这是我问题的前半部分.我的另一半问题与法布里奥指出的hbrock解决方案有关.

ear*_*tom 7

创建文件夹"C:\ inetpub\wwwroot\sites\mysite\App_Data"并将数据保存在那里.

System.IO.File.WriteAllLines(Server.MapPath("~/App_Data/log.txt"))
Run Code Online (Sandbox Code Playgroud)

如果您绝对必须将其保存到mysite目录,请注意将日志文件放在那里并适当设置目录权限的安全后果.运行IIS的用户需要能够读取和写入该目录.

完全符合条件的路径: System.Web.HttpContext.Current.Server