Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I configure my ASP.NET directory to allow writing a log file?

Tags:

c#

asp.net

iis

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.

like image 534
meanbunny Avatar asked Sep 12 '12 17:09

meanbunny


People also ask

How can I give permission to a folder in asp net?

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.

Where does ILogger write to?

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.

Where to find stdoutLogFile?

When publishing an app for Azure App Service deployment, the Web SDK sets the stdoutLogFile value to \\? \%home%\LogFiles\stdout .


2 Answers

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

like image 157
earth_tom Avatar answered Oct 03 '22 03:10

earth_tom


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.

like image 41
Fabio Avatar answered Oct 03 '22 04:10

Fabio