Click here to Skip to main content
15,887,975 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using the following codes for creating error log in an ASP.NET application. Those codes are working if I host the application in the IIS of the development machine. But, if I host it in another IIS, the error log is not created. Someone please help me

MIDL
public void WriteError([System.Runtime.InteropServices.OptionalAttribute, System.Runtime.InteropServices.DefaultParameterValueAttribute("")]  
string ErrorNumber, [System.Runtime.InteropServices.OptionalAttribute, System.Runtime.InteropServices.DefaultParameterValueAttribute("")] 
string ErrorMessage, [System.Runtime.InteropServices.OptionalAttribute, System.Runtime.InteropServices.DefaultParameterValueAttribute("")] 
string ErrorSource, [System.Runtime.InteropServices.OptionalAttribute, System.Runtime.InteropServices.DefaultParameterValueAttribute("")] 
string ErrorStackTrace)

{
C#
string path = "~/Error/" + DateTime.Today.ToString("dd-MM-yyyy") + ".txt";
if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(path)))
{
    File.Create(System.Web.HttpContext.Current.Server.MapPath(path)).Close();
}

using (StreamWriter w = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(path)))
{
    w.Write("@" + "Log Entry : ");
    w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
    w.WriteLine("Error# :{0}", ErrorNumber);
    w.WriteLine("Message :{0}", ErrorMessage);
    w.WriteLine("Source :{0}", ErrorSource);
    w.WriteLine("StackTrace :{0}", ErrorStackTrace);
    w.Flush();
    w.Close();
}

}
Posted
Updated 25-Jan-10 1:05am
v3

You probably need to enable write permissions for the folder in question.
 
Share this answer
 
Looks like:
File path is not accessible or the folder does not has full security rights in order to create the log file.

In case log file has been created explicitly, the write permissions are not provided.

Provide the proper security permissions and the issue should get resolved.
 
Share this answer
 
The code seems okay to me assuming parameters are passed correctly.
Can you edit the question to include the method signature (There is a reason I am asking this.. )
 
Share this answer
 
v2
I have enabled the write permission of that folder from IIS but still its not working.
 
Share this answer
 
MIDL
public void WriteError([System.Runtime.InteropServices.OptionalAttribute, System.Runtime.InteropServices.DefaultParameterValueAttribute("")]  
string ErrorNumber, [System.Runtime.InteropServices.OptionalAttribute, System.Runtime.InteropServices.DefaultParameterValueAttribute("")] 
string ErrorMessage, [System.Runtime.InteropServices.OptionalAttribute, System.Runtime.InteropServices.DefaultParameterValueAttribute("")] 
string ErrorSource, [System.Runtime.InteropServices.OptionalAttribute, System.Runtime.InteropServices.DefaultParameterValueAttribute("")] 
string ErrorStackTrace)
 
Share this answer
 
v2
Since you are using ASP.net, you can display an error message to the client. Catch the exception thrown by the error writing code and display that to the client. If you still can't figure out what the problem is, update your question with details from the exception.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900