Ok so I believe I am doing something very easy here. I have written an ASP.NET web page and it is just trying to write to the local directory.
I am using the following code:
System.IO.File.WriteAllLines("log.txt", messages);
I am throwing the following exception.
Access to the path 'c:\windows\system32\inetsrv\log.txt' is denied.
My ASP.NET application sits in the following directory.
c:\inetpub\wwwroot\sites\mysite\
So I am confused as to why it is trying to write to c:\windows\system32\inetsrv\ directory when I am not supplying that directory itself.
I have tried changing the code to the following but it gives me the same error message with a new directory.
System.IO.File.WriteAllLines("c:\\inetpub\\wwwroot\\sites\mysite\log.txt", messages);
Edit 1
It was hard to accept an answer on this because everyone really helped me out a ton. I accepted tom_yes_tom's answer because he was the first to post his response which was the first half of my problem. The other half of my problem was related to hbrock's solution that Fabio pointed out.
In File Explorer, navigate to C:\inetpub\wwwroot\ContosoUniversity. Right-click the Elmah folder, select Properties, and then select the Security tab. Click Edit. In the Permissions for Elmah dialog box, select DefaultAppPool, and then select the Write check box in the Allow column.
ILogger offers provider-based functionality to write to the console, the debug window, the Windows Event Log, to Microsoft Azure Ap Services diagnostics and logs, as well as to the TraceSource and the EventSource.
When publishing an app for Azure App Service deployment, the Web SDK sets the stdoutLogFile value to \\? \%home%\LogFiles\stdout .
Create the folder "C:\inetpub\wwwroot\sites\mysite\App_Data" and save your data there instead.
System.IO.File.WriteAllLines(Server.MapPath("~/App_Data/log.txt"))
If you absolutely must save it to the mysite directory, be aware of security ramifications of putting a log file there and set directory permissions appropriately. The user that IIS is running under needs to be able to read and write that directory.
Full qualifying path: System.Web.HttpContext.Current.Server
You're receiving "Access to the path 'c:\windows\system32\inetsrv\log.txt' is denied." when you execute System.IO.File.WriteAllLines("log.txt", messages) because c:\windows\system32\inetsrv is the directory where the IIS executable is located (w3wp.exe).
You have to use Server.MapPath so it gives you the physical path of your virtual directory.
Look which user is running your virtual directory's application pool, and give him write permissions on the folder of your virtual directory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With