How to create a log error file in ASP.NET Programming Language?

Asked 21-Oct-2018
Viewed 680 times

0

How to create a log error file in ASP.NET Programming Language?


1 Answer


0

“Log file”
In any programming language for tracking our application performance as well as for maintaining the history of any bugs or error, we generally create a log file in any temporary folder. That helps us to
save our time for easily finding the bugs in a whole application.

Here is the simple example where we can create a log error file in ASP.NET programming language.

using System; 

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
public partial class _Defaut:System.Web.UI.page
{
protect void page_load(object sender,EventArgs e)
{
try{
var i=10;
var j=0;
var result= i/j;
Responce.Write(result);
}
catch(Exception e){
lblMessage.Text=“Exception generated! Plz check your Log Error File”;
createLogErrorFile(e.ToString());
}
}
private void createLogErrorFile(String errMessage) {
try{
string path=”~/”+DateTime.ToDay.ToString(“dd-mm-yy”)+”.txt”;
if(!File.Exists(System.Web.HttpContext.Current.Server.MathPath(path))
{
File.create(System.Web.HttpContext.Current.Server.MathPath(path)).close();
}
Using(StreamWriter w= File.AppenedText(System.Web.HttpContext.Current.Server.MathPath(path)))
w.WriteLine(“|n |n Log Entry : “);
w.WriteLine(“{0}”, DateTime.Now.ToString(CultureInfo.InvariantCulture));
String err=”Error in : “+System.Web.HttpContext.Current.Request.Url.ToString()+”|n |n Error Message : ”+ errMessage;
w.WriteLine(err);
W.WriteLine(“===========================================”);
w.Flush();
w.close();
}
catch{
throw;
}
}
}

Log File : 21-10-18.txt

Log Entry :
21/10/2018 11 : 37 : 15
Error In : ..........................................................
Error Message : ................................................
======================================.


Comment
Thank You for the answer. - Anonymous User18-Jan-2019