自定义日志
作者:互联网
适用于小型项目,自定义日志方法如下
1 public static void WriteLogs(string content) 2 { 3 string path = AppDomain.CurrentDomain.BaseDirectory; 4 string LogName = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Namespace.Split('.')[0]; 5 string[] sArray = path.Split(new string[] { LogName }, StringSplitOptions.RemoveEmptyEntries); 6 string aa = sArray[0] + "\\" + LogName + "Log\\"; 7 path = aa; 8 if (!string.IsNullOrEmpty(path)) 9 { 10 if (!Directory.Exists(path)) 11 { 12 Directory.CreateDirectory(path); 13 } 14 path = path + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";// 15 if (!File.Exists(path)) 16 { 17 FileStream fs = File.Create(path); 18 fs.Close(); 19 } 20 if (File.Exists(path)) 21 { 22 StreamWriter sw = new StreamWriter(path, true, System.Text.Encoding.Default); 23 sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "----" + content + "\r\n"); 24 sw.Close(); 25 } 26 } 27 }
标签:string,Exists,sw,LogName,File,path,日志,自定义 来源: https://www.cnblogs.com/ywkcode/p/10896262.html