StreamWrite / Problem?

L

Leon

I wrote an custom error handler that works, but why the ErrorLog.txt file is
being created/written in the root web instead of the folder within my web
application?
Root web = C:\Inetpub\wwwroot -&- Web Application Folder =
C:\Inetpub\wwwroot\MySite
See code:
Public Shared Sub LogError(ByVal message As String)
' Get the current HTTPContext
Dim context As HttpContext = HttpContext.Current
' Get location of ErrorLogFile from Web.config file
Dim filePath As String = context.Server.MapPath( "../../ErrorLog.txt")
' Write message to error file

Try
Dim sw As New System.IO.StreamWriter(filePath, True)
sw.WriteLine(message)
sw.WriteLine()
sw.Close()

Catch
' If error writing to file, simply continue
End Try

End Sub
End Class
End Namespace
and if I try to hard code write the error to my ErrorLog text file it in my
web application (such as "MySite/ErrorLog.txt") nothing gets written?
 
C

Carol Braileanu

Leon,

Avoid using parent paths. Instead of

Dim filePath As String = context.Server.MapPath( "../../ErrorLog.txt")


try

Dim filePath As String = context.Server.MapPath( "~/ErrorLog.txt")
 
J

Johann MacDonagh

Server.MapPath("") should return C:\Inetpub\wwwroot\MySite Therefore, you
should modify the Server.MapPath line to:

Dim filePath As String = context.Server.MapPath( "ErrorLog.txt")

That should create the filename C:\Inetpub\wwwroot\MySite\ErrorLog.txt

Just a few things. HttpContext.Current.Server and Page.Server (or just
Server if you are coding in the code behind) are the same object. You can
save yourself a few lines by removing the code to get the Current
HttpContext and using it's Server property.

Also, you should have the StreamWriter's close method in the Finally portion
of your Try / Catch. There's been many times when I've forgotten to close a
connection to a file / database when I hit an exception, and the database
(especially if you're using Access) doesn't allow connections for a while.

Hope I could help!

Happy coding,
Johann MacDonagh
 
L

Leon

Now I'm getting the following error, what should I do? I once read it is a
bad idea to give ASP.NET WRITE access to the C: drive.

Server Error in '/MySite' Application.
--------------------------------------------------------------------------------

Access to the path "c:\inetpub\wwwroot\MySite\ErrorLog.txt" is denied.
Exception Details: System.UnauthorizedAccessException: Access to the path
"c:\inetpub\wwwroot\MySite\ErrorLog.txt" is denied.

ASP.NET is not authorized to access the requested resource. Consider
granting access rights to the resource to the ASP.NET request identity.
ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or
Network Service on IIS 6) that is used if the application is not
impersonating. If the application is impersonating via <identity
impersonate="true"/>, the identity will be the anonymous user (typically
IUSR_MACHINENAME) or the authenticated request user.

To grant ASP.NET write access to a file, right-click the file in Explorer,
choose "Properties" and select the Security tab. Click "Add" to add the
appropriate user or group. Highlight the ASP.NET account, and check the
boxes for the desired access.
 
L

Leon

why adding the following line to my Web.config file?
<identity impersonate="true"/>
 
J

Johann MacDonagh

The user that the ASP.net process gets executed under (and therefore, the
user that needs permission to that ErrorLog.txt) depends on three different
areas. This article on MSDN will show you the setup you need to impersonate
the ASP.net process as the user you need. As for security, you can set the
ASPNET user to only allow write access to ErrorLog.txt, not the entire C
drive.

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

Do you need help setting up security on ErrorLog.txt?

Hope this helps,
Johann MacDonagh
 

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

Top