System IO used with ASP.NET

  • Thread starter Plop Plop via DotNetMonster.com
  • Start date
P

Plop Plop via DotNetMonster.com

Hello,

i'm a newbie at asp.net, and i'm developping a site at the moment, and want
to add a sort off logging system.

I would like to create a file on the server side when a error occured.
Now how can i do that?

On my pc i have c:\inetput\wwwroot\sitename

i want the file to become in this directory, but i can't use this path
because when i upload it to a host server, the path will be different. So
how can i make sure the file comes in the directory where my site is
located without statically typing in the path in a string orso?

Or thus someone has a better idea of solving this problem?

thank you
 
C

Craig Deelsnyder

Hello,

i'm a newbie at asp.net, and i'm developping a site at the moment, and
want
to add a sort off logging system.

I would like to create a file on the server side when a error occured.
Now how can i do that?

On my pc i have c:\inetput\wwwroot\sitename

i want the file to become in this directory, but i can't use this path
because when i upload it to a host server, the path will be different.
So
how can i make sure the file comes in the directory where my site is
located without statically typing in the path in a string orso?

Or thus someone has a better idea of solving this problem?

thank you

Check out HttpServerUtility.MapPath method, takes a virtual path on the
server and maps to an actual server path:

http://msdn.microsoft.com/library/d...stemwebhttpserverutilityclassmappathtopic.asp

Note a reference to HttpServerUtility is available thru the Server
property of your Page; i.e. to use this inside one of your pages:

//to create file in same dir as page
Server.MapPath("myFile.txt")

//to create file in parent directory
Server.MapPath("../myFile.txt")

etc
 
C

Craig Deelsnyder

Check out HttpServerUtility.MapPath method, takes a virtual path on the
server and maps to
an actual server path:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemwebhttpserverutilityclassmappathtopic.asp

Note a reference to HttpServerUtility is available thru the Server
property of your Page;
i.e. to use this inside one of your pages:

//to create file in same dir as page
Server.MapPath("myFile.txt")

//to create file in parent directory
Server.MapPath("../myFile.txt")

etc
 
P

Peter Rilling

One additional note is the concurrency may be hard to maintain when writing
to a file, unless each session has their own log file. Suppose two sessions
(users) encounter an error, and they both try to write to the file at the
same time. Two things can happen, 1) The first one to open the file will
also lock it, but then the second access attempt will error because the file
is locked, or 2) Both will try to write to the file at the same time and
the log entries will become mangled.

There are ways to mitigate this, but just wanted to point out the issues so
that you can design something appropriate. Another alternative might be to
log to the event log.
 
P

Plop Plop via DotNetMonster.com

hehe yep you are right about this.

But i would create a file when a major error occurs, so i never try to open
and append to an already existing file.

Would make a name from a timestamp, added with a session nr orso , i think
that would work?
 

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,995
Messages
2,570,226
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top