Create log file in asp.net using C#:
.NET have a various classes to create, write on a text file which are file .Here I'm using create() method to create a text file and using some classes to reading and writing text files those are TextReader,StreamReader,StreamWriter,TextWriter classes.These are abstract classes .Here i have taken a logfilepath which is the physical path of the text file.If the file is existing in path then we will write message into this text file.If the file is not existing in the path then we will create a file in this path and write a (log) messages into it
Code:
Please share this post if it's useful to you. Thanks!.
.NET have a various classes to create, write on a text file which are file .Here I'm using create() method to create a text file and using some classes to reading and writing text files those are TextReader,StreamReader,StreamWriter,TextWriter classes.These are abstract classes .Here i have taken a logfilepath which is the physical path of the text file.If the file is existing in path then we will write message into this text file.If the file is not existing in the path then we will create a file in this path and write a (log) messages into it
Code:
public static string strPath = AppDomain.CurrentDomain.BaseDirectory; public static string strLogFilePath = strPath + @"log\log.txt"; public static void WriteToLog(string msg) { try { if (!File.Exists(strLogFilePath)) { File.Create(strLogFilePath).Close(); } using (StreamWriter w = File.AppendText(strLogFilePath)) { w.WriteLine("\r\nLog: "); w.WriteLine("{0}",DateTime.Now.ToString(CultureInfo.InvariantCulture)); string err = "Error Message:" + msg; w.WriteLine(err); w.Flush(); w.Close(); } }
0 comments
Post a Comment