How to create a Log File?

Posted by Laghaterohan under Regular Expressions on 1/29/2011 | Points: 10 | Views : 13900 | Status : [Member] | Replies : 9
Hello,
May i please know how to create a log file (C#,asp.net 3.5). I want to keep a log file inorder to trace where my application fails / crashes at the deployment site? Can you please tell me what are the steps that i need to do in order to create a log file for this purpose?

Thanks!

Best Regards,
Rohan Laghate



Responses

Posted by: Karthikanbarasan on: 1/29/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Hi,

Check this excellent article by shivprasad... it gives you clear idea on tracing and logging

http://www.dotnetfunda.com/articles/article1142-debugging-tracing-and-instrumentation-in-net-and-aspnet-14-faq-.aspx

Thanks
Karthik
www.f5Debug.net

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Laghaterohan on: 1/29/2011 [Member] Starter | Points: 25

Up
0
Down
thanks for the reply..i had read that article...and its really nice..

however, am confused as to about how to create my own logfile...for the application which is already ready...

How to begin about creating a log file now what are the steps.....please help me out...


Thanks!

Best Regards,
Rohan Laghate

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabhakar on: 1/29/2011 [Member] [MVP] Starter | Points: 25

Up
0
Down
hi Laghaterohan

go with this article ... watch video it's help us..


http://unicomtech.blogspot.com/2010/07/notepad-editor-trick-to-create-log-file.html

Best Regard's
Prabhakar

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Laghaterohan on: 1/29/2011 [Member] Starter | Points: 25

Up
0
Down
Thanks Prabhakar...!

Best Regards,
Rohan Laghate

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabhakar on: 1/29/2011 [Member] [MVP] Starter | Points: 25

Up
0
Down
hi Laghaterohan

it's ok ... if ur problem short out so click Mark as Answer

Best Regard's
Prabhakar

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 1/29/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Hi,

To create your own Logfile...

using System.Collections.Generic;

using System.Web.UI;

public static class Logger
{
private static readonly Page Pge = new Page();
private static readonly string Path = Pge.Server.MapPath("~/yourLogPath/Log.txt");
private const string LineBreaker = "\r\n\r======================================================================================= \r\n\r";

public static void LogError(string myMessage, Exception e)
{
const LogSeverity severity = LogSeverity.Error;
string messageToWrite = string.Format("{0} {1}: {2} \r\n\r {3}\r\n\r {4}{5}", DateTime.Now, severity, myMessage, e.Message, e.StackTrace, LineBreaker);
System.IO.File.AppendAllText(Path, messageToWrite);
}
}


Thanks
Karthik
www.f5Debug.net

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shanthosh2110 on: 12/12/2011 [Member] Starter | Points: 25

Up
0
Down
hi karthi,

how to create the log file programatically.

Thanks in advance,
Regards,
Shanthosh

shanthosh.s

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksamantaray on: 12/13/2011 [Member] Silver | Points: 25

Up
0
Down
Hi,
You can have a text file in your project and then you can write dynamic code in Application_Error function of global.asax.So any error at any page happens anytime will go to this section and written to text file.

void Application_Error(object sender, EventArgs e)
{
Exception ex = new Exception();
ex = Server.GetLastError().GetBaseException();

System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath("~/error.txt"), System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite);
System.IO.StreamWriter s = new System.IO.StreamWriter(fs);
s.BaseStream.Seek(0, System.IO.SeekOrigin.End);
s.WriteLine("MESSAGE: " + ex.Message + "\nSOURCE: " + ex.Source + "\nFORM: " + Request.Form.ToString() + "\nQUERYSTRING: " + Request.QueryString.ToString() + "\nTARGETSITE: " + ex.TargetSite.ToString() + "\nSTACKTRACE: " + ex.StackTrace + System.Diagnostics.EventLogEntryType.Error);
s.WriteLine("--------------------------------------------------------");
s.Close();
}
Hope this will help you.

Thanks,
Sanjay

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shanthosh2110 on: 12/20/2011 [Member] Starter | Points: 25

Up
0
Down
Thanks but i created it in your way only

shanthosh.s

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response