Ghi log lỗi
csharp
1try2 {3 // Lỗi runtime~4 int a = 10;5 int b = 0;6 7 int c = a / b;8 }9 catch (Exception ex)10 {11 Console.WriteLine("Opps! Co loi xay ra! Vui long lien he Quan tri he thong (0915659223)!");12 Console.WriteLine("Vui long goi file log trong thu muc logs den Nha cung cap de khac phuc su co!");13 14 // Ghi log15 string logFolderName = "logs/";16 if (!Directory.Exists(logFolderName))17 {18 // Kiểm tra thư mục log có tồn tại không? Nếu chưa có thì tạo mới19 Directory.CreateDirectory(logFolderName);20 }21 string logFileName = ""; //2019_11_16_log.txt22 DateTime now = DateTime.Now;23 logFileName = String.Format("{0}_{1}_{2}_log.txt",24 now.Year, now.Month, now.Day);25 26 // logs/2019_11_16_log.txt27 //string fullFileLog = logFolderName + "/" + logFileName;28 string fullFileLog = Path.Combine(logFolderName, logFileName);29 30 using (StreamWriter sw = new StreamWriter(fullFileLog))31 {32 sw.WriteLine(String.Format("Loi xay ra vao luc: {0}", now));33 sw.WriteLine(String.Format("Loi cu the: {0}", ex.Message));34 }35 36 // Tu dong am tham goi file log nay ve Server cua ban~37 }
