Recently, a simple file log component is required. log4net configuration is too troublesome. I found some information on the Internet and improved it.
The main code is as follows:
private static void WriteLog(string message) { try { string rootPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; if (!rootPath.EndsWith("\\")) { rootPath += "\\"; } string path = rootPath + "Log\\"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string filePath = path + DateTime.Today.ToString("yyyy-mm-dd") + ".txt"; if (!File.Exists(filePath)) { File.Create(filePath).Close(); } using (StreamWriter w = File.AppendText(filePath)) { w.WriteLine("\r\n"); w.WriteLine("{0} ", DateTime.Now.ToString(CultureInfo.InvariantCulture)); w.WriteLine(message); w.Flush(); w.Close(); } } catch (Exception ex) { } }
Reference: error logging using ASP. NET 2.0
File Download: Download