Ghi log lỗi
try
{
// Lỗi runtime~
int a = 10;
int b = 0;
int c = a / b;
}
catch (Exception ex)
{
Console.WriteLine("Opps! Co loi xay ra! Vui long lien he Quan tri he thong (0915659223)!");
Console.WriteLine("Vui long goi file log trong thu muc logs den Nha cung cap de khac phuc su co!");
// Ghi log
string logFolderName = "logs/";
if (!Directory.Exists(logFolderName))
{
// Kiểm tra thư mục log có tồn tại không? Nếu chưa có thì tạo mới
Directory.CreateDirectory(logFolderName);
}
string logFileName = ""; //2019_11_16_log.txt
DateTime now = DateTime.Now;
logFileName = String.Format("{0}_{1}_{2}_log.txt",
now.Year, now.Month, now.Day);
// logs/2019_11_16_log.txt
//string fullFileLog = logFolderName + "/" + logFileName;
string fullFileLog = Path.Combine(logFolderName, logFileName);
using (StreamWriter sw = new StreamWriter(fullFileLog))
{
sw.WriteLine(String.Format("Loi xay ra vao luc: {0}", now));
sw.WriteLine(String.Format("Loi cu the: {0}", ex.Message));
}
// Tu dong am tham goi file log nay ve Server cua ban~
}
|