为App添加Log日志文件

很多时候需要为我们应用程序添加日子文件,方便问题定位

代码转载而来,非原创

为App添加Log日志文件
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.IO;
  6 using System.Reflection;
  7 
  8 namespace CommonLib
  9 {
 10     /// <summary>
 11     /// 日志类型
 12     /// </summary>
 13     public enum LogType
 14     {
 15         All,
 16         Information,
 17         Debug,
 18         Success,
 19         Failure,
 20         Warning,
 21         Error
 22     }
 23 
 24 
 25     public class Logger
 26     {
 27 
 28         #region Instance
 29         private static object logLock;
 30 
 31         private static Logger _instance;
 32 
 33         private static string logFileName;
 34         private Logger() { }
 35 
 36         /// <summary>
 37         /// Logger instance
 38         /// </summary>
 39         public static Logger Instance
 40         {
 41             get
 42             {
 43                 if (_instance == null)
 44                 {
 45                     _instance = new Logger();
 46                     logLock = new object();
 47                     //logFileName = Guid.NewGuid() + ".log";
 48                     logFileName = DateTime.Now.Hour.ToString("00") + DateTime.Now.Minute.ToString("00") +DateTime.Now.Second.ToString("00") + ".log";
 49                 }
 50                 return _instance;
 51             }
 52         }
 53         #endregion
 54 
 55         /// <summary>
 56         /// Write log to log file
 57         /// </summary>
 58         /// <param name="logContent">Log content</param>
 59         /// <param name="logType">Log type</param>
 60         public void WriteLog(string logContent, LogType logType = LogType.Information, string fileName = null)
 61         {
 62             try
 63             {
 64                 string basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
 65                 basePath = Directory.GetCurrentDirectory(); //@"C:\APILogs";
 66                 if (!Directory.Exists(basePath + "\\Log"))
 67                 {
 68                     Directory.CreateDirectory(basePath + "\\Log");
 69                 }
 70 
 71                 string dataString = DateTime.Now.ToString("yyyy-MM-dd");
 72                 if (!Directory.Exists(basePath + "\\Log\\" + dataString))
 73                 {
 74                     Directory.CreateDirectory(basePath + "\\Log\\" + dataString);
 75                 }
 76 
 77                 string[] logText = new string[] { DateTime.Now.ToString("hh:mm:ss") + ": " + logType.ToString() + ": " + logContent };
 78                 if (!string.IsNullOrEmpty(fileName))
 79                 {
 80                     fileName = fileName + "_" + logFileName;
 81                 }
 82                 else
 83                 {
 84                     fileName = logFileName;
 85                     //fileName = DateTime.Now.ToString("hh:mm:ss");
 86                 }
 87 
 88                 lock (logLock)
 89                 {
 90                     File.AppendAllLines(basePath + "\\Log\\" + dataString + "\\" + fileName, logText);
 91                 }
 92             }
 93             catch (Exception) { }
 94         }
 95 
 96         /// <summary>
 97         /// Write exception to log file
 98         /// </summary>
 99         /// <param name="exception">Exception</param>
100         public void WriteException(Exception exception, string specialText = null)
101         {
102             if (exception != null)
103             {
104                 Type exceptionType = exception.GetType();
105                 string text = string.Empty;
106                 if (!string.IsNullOrEmpty(specialText))
107                 {
108                     text = text + specialText + Environment.NewLine;
109                 }
110                 text = "Exception: " + exceptionType.Name + Environment.NewLine;
111                 text += "               " + "Message: " + exception.Message + Environment.NewLine;
112                 text += "               " + "Source: " + exception.Source + Environment.NewLine;
113                 text += "               " + "StackTrace: " + exception.StackTrace + Environment.NewLine;
114                 WriteLog(text, LogType.Error);
115             }
116         }
117 
118     }
119 }
View Code

 

 

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.IO;
  6 using System.Reflection;
  7 
  8 namespace CommonLib
  9 {
 10     /// <summary>
 11     /// 日志类型
 12     /// </summary>
 13     public enum LogType
 14     {
 15         All,
 16         Information,
 17         Debug,
 18         Success,
 19         Failure,
 20         Warning,
 21         Error
 22     }
 23 
 24 
 25     public class Logger
 26     {
 27 
 28         #region Instance
 29         private static object logLock;
 30 
 31         private static Logger _instance;
 32 
 33         private static string logFileName;
 34         private Logger() { }
 35 
 36         /// <summary>
 37         /// Logger instance
 38         /// </summary>
 39         public static Logger Instance
 40         {
 41             get
 42             {
 43                 if (_instance == null)
 44                 {
 45                     _instance = new Logger();
 46                     logLock = new object();
 47                     //logFileName = Guid.NewGuid() + ".log";
 48                     logFileName = DateTime.Now.Hour.ToString("00") + DateTime.Now.Minute.ToString("00") +DateTime.Now.Second.ToString("00") + ".log";
 49                 }
 50                 return _instance;
 51             }
 52         }
 53         #endregion
 54 
 55         /// <summary>
 56         /// Write log to log file
 57         /// </summary>
 58         /// <param name="logContent">Log content</param>
 59         /// <param name="logType">Log type</param>
 60         public void WriteLog(string logContent, LogType logType = LogType.Information, string fileName = null)
 61         {
 62             try
 63             {
 64                 string basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
 65                 basePath = Directory.GetCurrentDirectory(); //@"C:\APILogs";
 66                 if (!Directory.Exists(basePath + "\\Log"))
 67                 {
 68                     Directory.CreateDirectory(basePath + "\\Log");
 69                 }
 70 
 71                 string dataString = DateTime.Now.ToString("yyyy-MM-dd");
 72                 if (!Directory.Exists(basePath + "\\Log\\" + dataString))
 73                 {
 74                     Directory.CreateDirectory(basePath + "\\Log\\" + dataString);
 75                 }
 76 
 77                 string[] logText = new string[] { DateTime.Now.ToString("hh:mm:ss") + ": " + logType.ToString() + ": " + logContent };
 78                 if (!string.IsNullOrEmpty(fileName))
 79                 {
 80                     fileName = fileName + "_" + logFileName;
 81                 }
 82                 else
 83                 {
 84                     fileName = logFileName;
 85                     //fileName = DateTime.Now.ToString("hh:mm:ss");
 86                 }
 87 
 88                 lock (logLock)
 89                 {
 90                     File.AppendAllLines(basePath + "\\Log\\" + dataString + "\\" + fileName, logText);
 91                 }
 92             }
 93             catch (Exception) { }
 94         }
 95 
 96         /// <summary>
 97         /// Write exception to log file
 98         /// </summary>
 99         /// <param name="exception">Exception</param>
100         public void WriteException(Exception exception, string specialText = null)
101         {
102             if (exception != null)
103             {
104                 Type exceptionType = exception.GetType();
105                 string text = string.Empty;
106                 if (!string.IsNullOrEmpty(specialText))
107                 {
108                     text = text + specialText + Environment.NewLine;
109                 }
110                 text = "Exception: " + exceptionType.Name + Environment.NewLine;
111                 text += "               " + "Message: " + exception.Message + Environment.NewLine;
112                 text += "               " + "Source: " + exception.Source + Environment.NewLine;
113                 text += "               " + "StackTrace: " + exception.StackTrace + Environment.NewLine;
114                 WriteLog(text, LogType.Error);
115             }
116         }
117 
118     }
119 }

 

为App添加Log日志文件

上一篇:怎么隐藏 iOS Safari 打开网页时的地址栏和工具栏探索


下一篇:ansible模块介绍之ios_facts