码迷,mamicode.com
首页 > 其他好文 > 详细

自定义日志

时间:2019-05-20 21:00:13      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:color   def   coding   stat   type   creat   方法   domain   ase   

适用于小型项目,自定义日志方法如下
    
 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         }

 

自定义日志

标签:color   def   coding   stat   type   creat   方法   domain   ase   

原文地址:https://www.cnblogs.com/ywkcode/p/10896262.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!