Access denied when writting to Application directory

J

José Joye

Hello,

I know the problem I'm having has been mentioned several time...
However, the answers provided are not crystal clear.

Here is my problem:
I'm building a small web service that I will host on an internet Server
provider site (eg: WebMatrixHosting)
To be able to trace how it reacts, I want to write information in a log
file. To do so, I want to use a tracelistener and write the information in a
log file located in the application directory.
Here is a snippet of what I do:

gLogFileSwitch = new TraceSwitch("LogFileSwitch", "Local File Trace
Switch");
HttpRequest currRequest = HttpContext.Current.Request;
string strPath = currRequest.MapPath(currRequest.ApplicationPath);
Trace.Listeners.Add(new TextWriterTraceListener(strPath +
@"\SmallGame.log"));

Doing this, I receive an "access denied error".
What is the correct way to go?

Thanks,
José
 
M

Martin Maat

I want to write information in a log file. To do so, I want to
use a tracelistener and write the information in a log file
located in the application directory. [...]
Doing this, I receive an "access denied error".
What is the correct way to go?

You need you loosen security settings on your log file, the account
IUSR_MACHINENAME needs write access to the file.

Create the file manually (e.g. using Notepad).
Right-click the file, choose Properties.
On the Security tab, add IUSR_MACHINENAME (MACHINENAME being your machine's
name).
Allow Read and Write access for this account.

Martin.
 
J

José Joye

Thanks to both of you for the answer,

Applying your proposal solve the problem.
I will also update an XML file (where I will store usage information).
Is this the way to go. I mean : create initial files and give access to
those files?

Thanks,
José

Martin Maat said:
I want to write information in a log file. To do so, I want to
use a tracelistener and write the information in a log file
located in the application directory. [...]
Doing this, I receive an "access denied error".
What is the correct way to go?

You need you loosen security settings on your log file, the account
IUSR_MACHINENAME needs write access to the file.

Create the file manually (e.g. using Notepad).
Right-click the file, choose Properties.
On the Security tab, add IUSR_MACHINENAME (MACHINENAME being your machine's
name).
Allow Read and Write access for this account.

Martin.
 
M

Martin Maat

José Joye said:
Thanks to both of you for the answer,

Applying your proposal solve the problem.
I will also update an XML file (where I will store usage information).
Is this the way to go. I mean : create initial files and give access to
those files?

It is the most straightforward way. If you intend to have a lot of files to
be written to that way and you need to create them from code, you may want
to try to set access rights for a folder and create all files in that
folder. My suggestion can get a bit cumbersome, especially if you deploy the
site to another machine or have to reinstall the lot for some reason.

Another thing to be aware of is synchronization. As your site gets more hits
and they come in simultaniously, the file may not be available for writing
because it is being written to on behalf of another visitor. You may want to
use a static XmlDocument member in your application class and use lock
statements with that or use mutexes to keep requests from getting in each
others' ways.

Martin.
 
J

José Joye

Thanks for your detailled answer.

José
Martin Maat said:
It is the most straightforward way. If you intend to have a lot of files to
be written to that way and you need to create them from code, you may want
to try to set access rights for a folder and create all files in that
folder. My suggestion can get a bit cumbersome, especially if you deploy the
site to another machine or have to reinstall the lot for some reason.

Another thing to be aware of is synchronization. As your site gets more hits
and they come in simultaniously, the file may not be available for writing
because it is being written to on behalf of another visitor. You may want to
use a static XmlDocument member in your application class and use lock
statements with that or use mutexes to keep requests from getting in each
others' ways.

Martin.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top