none
Unable to create file from Application_Start() event. RRS feed

  • Question

  • Hi,

    (Sorry if this is in the wrong forum, but I couldn't find a better one.)

    I have some code which attempts to create and write to a log file in a subdirectory of my website (shown below). If I put the first call to this code in the Page_Load() event of Default.aspx, it works fine every time. If, however, I make the first call to this code inside the Global.asax Application_Start() event, I get an access denied exception from .NET, even though the path displayed in the exception message is correct and is a subdirectory of my site root.

    Has anyone encountered anything like this? It is as if the code running from Application_Start() has a more limited set of permissions than code running later.

    Kind wishes ~ Patrick

    string s = string.Format( "{0}logs\\Log.txt", HttpRuntime.AppDomainAppPath );
    using( StreamWriter writer = new StreamWriter( s, true ) )
    {
    	string logEntry = DateTime.Now.ToString() + ": " + formattedMessage;
    	writer.WriteLine( logEntry );
    	writer.Flush();
    }
    


    Wednesday, January 29, 2014 5:53 PM

Answers

  • I would recommend that you take a look at who is creating the directory and/or file.  If the file already exists then the account under which IIS is running would need to have write access to it.  If you're using Win auth then while the request is being handled the context will be the Win user and not the IIS app pool account.  This could cause problems depending upon the NTFS permissions.
    Thursday, January 30, 2014 4:35 PM
    Moderator

All replies

  • I'm not aware of any issues accessing the property.  It works fine for me.  But the directory structure must already exist otherwise it will fail.  Are you sure the directory path ({0}logs) is valid.  You might need to create the directory first.

    var path = Path.Combine(HttpRuntime.AppDomainAppPath, "Logs");
    if (!Directory.Exists(path))
        Directory.CreateDirectory(path);
                
    string s = Path.Combine(path, "Log.txt");

    Please post ASP.NET questions in the ASP.NET forums (http://forums.asp.net ).

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Wednesday, January 29, 2014 7:08 PM
    Moderator
  • Hi,

    Yes, the folder definitely exists.  The same code run from Page_Load of Default.aspx creates and writes to the file fine.  It seems Application_Start does not have the same permissions as code running later.

    - Patrick

    Thursday, January 30, 2014 4:25 PM
  • I would recommend that you take a look at who is creating the directory and/or file.  If the file already exists then the account under which IIS is running would need to have write access to it.  If you're using Win auth then while the request is being handled the context will be the Win user and not the IIS app pool account.  This could cause problems depending upon the NTFS permissions.
    Thursday, January 30, 2014 4:35 PM
    Moderator