0
Posted on 11:22 PM by prajeesh and filed under ,
It is a good idea to create log files to identify exceptions occured in your webapplication in a live environment.
In ASP.net we can achieve this by creating a Text file in the application and write exceptions into the text file with time of exception, here is the sample code:
First you have to create a Log.txt file in the root folder of your application
try
{
//lines of code that may generate exceptions.
}
catch(Exception ex)
{
if (System.IO.File.Exists(HttpContext.Current.Request.ApplicationPath + "/Log.txt"));
{
System.IO.StreamWriter Sr = new System.IO.StreamWriter(HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath+"/Log.txt"),true);
Sr.WriteLine(
DateTime.Now.ToString()+":"+ex.ToString());
}
}

so all exceptions generated within the try block will be recorded into Log.txt file.


Shout it kick it on DotNetKicks.com
0
Responses to ... Creating a log file in asp.net application