.NET Framework Bookmark and Share   
 index > .NET Base Class Library > Access Denied when logging to text file
 

Access Denied when logging to text file

I am trying to log to a local text file from a web app. Here is my code:

Code Block

public static void writeToLog(string message)
{
try
{
//Verify today's log file exists in the log directory, if not create it
if (File.Exists(_logPath + _logName) == false)
{
FileStream os = new FileStream(_logPath + _logName, FileMode.Append, FileAccess.Write);
os.Close();
}

//StreamWriter sw = new StreamWriter(_logPath + _logName, true);
StreamWriter sw = File.AppendText(_logPath + _logName);

sw.WriteLine(DateTime.Now + "\t" + message);
sw.Flush();
sw.Close();
}
catch (Exception ex)
{
//Email.emailTo(ConfigurationManager.AppSettings["sentTo"], ConfigurationManager.AppSettings["sentFrom"], "MagicCaseAcceptor Log Error", ex.Message);
}
}



This method is in a Log class and from my main program I am calling it like
Log.writeToLog("some message");
The problem I'm running into is that I'm getting random "Access Denied" errors when initializing the stream writer. So I end up with a log that is missing entries. Yes, ASPNET has privileges on the folder to write to the file and does sporadically. Any ideas what I'm doing wrong?
pidass

Yep, your not locking the file to force other parts of the code attemtping to log to wait.

You check the file exists, if not create it and if it does you open it, what if anotehr program has opened the file for editing. You get your access denied problem, I can't guarantee this is the issue from your code all I can say is you have not shown code where you do lock the file or where you check if the file is locked.

So it's possible the file is not always released properly or another program, or even the same one is already writing to the file.

Zamial

Hi,

Your codedoes not guarantee the close of the file, for the close operation is not in a Finally block.

Code Block

public static void writeToLog(string message)
{
FileStream fs = null;
StreamWriter sw = null;

try
{
fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.Read);
sw = new StreamWriter(fs);

sw.WriteLine(message);
}
catch (Exception ex)
{
//...
}
finally
{
if (sw != null)
sw.Close();
if (fs != null)
fs.Close();
}
}

Using the existing file stream can reduce the chance of collision and also make the code more concise.

Howeverthis code can not avoid the collision completely. There may be two clients attempting to write to the same file. You may choose to synchronise the file write in certain way. Considering that it is a web application and it is not appropriate to let the program wait for the file, you may choose to write it to a database such as SQL Serverinstead.

Best Regards

Chunsheng Tang

Chunsheng Tang

Yep, your not locking the file to force other parts of the code attemtping to log to wait.

You check the file exists, if not create it and if it does you open it, what if anotehr program has opened the file for editing. You get your access denied problem, I can't guarantee this is the issue from your code all I can say is you have not shown code where you do lock the file or where you check if the file is locked.

So it's possible the file is not always released properly or another program, or even the same one is already writing to the file.

Zamial

Hi,

Your codedoes not guarantee the close of the file, for the close operation is not in a Finally block.

Code Block

public static void writeToLog(string message)
{
FileStream fs = null;
StreamWriter sw = null;

try
{
fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.Read);
sw = new StreamWriter(fs);

sw.WriteLine(message);
}
catch (Exception ex)
{
//...
}
finally
{
if (sw != null)
sw.Close();
if (fs != null)
fs.Close();
}
}

Using the existing file stream can reduce the chance of collision and also make the code more concise.

Howeverthis code can not avoid the collision completely. There may be two clients attempting to write to the same file. You may choose to synchronise the file write in certain way. Considering that it is a web application and it is not appropriate to let the program wait for the file, you may choose to write it to a database such as SQL Serverinstead.

Best Regards

Chunsheng Tang

Chunsheng Tang

Also you did say local file from a web app however I think the file will be on the web server itself not the local machine, it will be the local machine whilst developing as you are the server and the client.

But when you release it the log file will be on the server only.

If my memory servers me correctly Smile

Zamial

You can use google to search for other answers

Custom Search

More Threads

• How to do Hardware interface in C#
• List.FindAll anonymous method
• Switching EventLogs for your application
• System.Reflection.Emit call to private method
• Connecting to LDAP in .NET 2.0 or .NET 3.5
• Reciving an event when program exit
• Dynamic classes and VS.NET and Intellisense real-time support
• DVD Burning component
• Combining Images
• How to set background color in databound listbox