Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

write logfile to server using asp.net 1

Status
Not open for further replies.

PC888

Programmer
Nov 7, 2003
117
CA
Would someone direct me to a code example by which I can append information, date and time of visit, for example, to a logfile on the server using .asp and if possible using Javascript or VB.net.
Many thanks.
 
I copied this from another thread - haven't had the need for it yet but thought it might point you in the right direction.

Write to Log Code:
System.Diagnostics.EventLog.WriteEntry("Source", "Message")
This will put info to the servers event log

Hope this helps.

Hope everyone is having a great day!

Thanks - Jennifer
 
Thanks Jennifer, it sure will give me a good point to start.

As I am trying to have a log for particular pages, and I do not have access to the server's event log, I wonder if you might have come across another thread that appends information to a user file, like an ascii log file.

I have considered making a link to an excel spreadsheet or even an access database, but I thought there may be a simpler way.
 
This should work
Code:
		public void WriteToWebLogFile(StringWriter sw) 
		{
			using(StreamWriter stw = new StreamWriter(System.Web.HttpContext.Current.Server.MapPath("error.txt"), true)) 
				try
				{
					stw.WriteLine(sw.ToString());
				} 
				catch(Exception Ex)
				{
					System.Web.HttpContext.Current.Response.Write(Ex.ToString());
				}
		}
This builds a StringWriter that I Pass in
Code:
		public SkillException(SqlException sqlex)
		{
			StringWriter sw_LogMsg = new StringWriter();
			sw_LogMsg.WriteLine("Date and Time of SqlException " + DateTime.Now.ToString());
			sw_LogMsg.WriteLine("Exception Number : " + sqlex.Number + 
			"Message : " + sqlex.Message + " has occurred");
			WriteToWebLogFile( sw_LogMsg );
			
			foreach (SqlError sqle in sqlex.Errors)
			{
				sw_LogMsg.WriteLine("Message: " + sqle.Message);
				sw_LogMsg.WriteLine("Number: " + sqle.Number);
				sw_LogMsg.WriteLine("Procedure: " + sqle.Procedure);
				sw_LogMsg.WriteLine("Server: " + sqle.Server);
				sw_LogMsg.WriteLine("Source: " + sqle.Source);
				sw_LogMsg.WriteLine("State: " + sqle.State);
				sw_LogMsg.WriteLine("Severity: " + sqle.Class);
				sw_LogMsg.WriteLine("LineNumber: " + sqle.LineNumber);
				WriteToWebLogFile(sw_LogMsg);
			}
			System.Web.HttpContext.Current.Server.Transfer("~/errorpage.aspx");
		}
Hope it helps,
Marty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top