1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
| using System; using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Linq; using System.Reflection; using System.Text;
namespace JohnSun.Utility { [Flags] public enum LogType { None = 0, File = 1, Console = 2, All = 3 }
[Flags] public enum LogLevel { [Description("None")] None = 0, [Description("跟踪")] Trace = 1, [Description("调试")] Debug = 2, [Description("消息")] Info = 4, [Description("警告")] Warn = 8, [Description("错误")] Error = 16, [Description("致命")] Fatal = 32, [Description("All")] All = 63 }
public class Logger { private static readonly Dictionary<LogLevel, string> _logLevelDescription = new Dictionary<LogLevel, string>();
public static Dictionary<LogLevel, string> LogLevelDescription { get { if (_logLevelDescription.Count == 0) { Array values = Enum.GetValues(typeof(LogLevel)); foreach (object value in values) { LogLevel level = (LogLevel)value; FieldInfo info = typeof(LogLevel).GetField(level.ToString()); object[] attrs = info.GetCustomAttributes(false); if (attrs.ToList().Find(attr => attr is DescriptionAttribute) is DescriptionAttribute attribute) { _logLevelDescription[level] = attribute.Description; } else { _logLevelDescription[level] = level.ToString(); } } } return _logLevelDescription; } }
public static LogLevel LogLevel { get; set; } = LogLevel.All;
public static LogType LogType { get; set; } = LogType.File;
private static void LogToConsole(string message, ConsoleColor textColor) { Console.OutputEncoding = Encoding.UTF8; Console.ForegroundColor = textColor; Console.WriteLine(message); Console.ResetColor();
Console.OutputEncoding = Encoding.Default; }
private static void LogToConsole(string message, ConsoleColor textColor, ConsoleColor backColor) { Console.OutputEncoding = Encoding.UTF8; Console.ForegroundColor = textColor; Console.BackgroundColor = backColor; Console.Write(message); Console.ResetColor(); Console.WriteLine();
Console.OutputEncoding = Encoding.Default; }
static void LogToConsole(LogLevel v, string message) { switch (v) { case LogLevel.Trace: LogToConsole(message, ConsoleColor.Black, ConsoleColor.DarkYellow); break; case LogLevel.Debug: LogToConsole(message, ConsoleColor.Cyan); break; case LogLevel.Info: LogToConsole(message, ConsoleColor.Yellow); break; case LogLevel.Warn: LogToConsole(message, ConsoleColor.Red); break; case LogLevel.Error: LogToConsole(message, ConsoleColor.Gray, ConsoleColor.Red); break; case LogLevel.Fatal: LogToConsole(message, ConsoleColor.Yellow, ConsoleColor.Red); break; } }
private static readonly object _sync = new object();
static void LogToFile(LogLevel v, string message) { string directory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs"); string path = Path.Combine(directory, DateTime.Now.ToString("yyyy-MM-dd") + ".txt"); lock (_sync) { if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } File.AppendAllText(path, message, Encoding.UTF8); } }
static void Log(LogLevel v, string message, Exception exception = null) { if ((v & LogLevel) == 0) return; if ((LogType & LogType.Console) != 0) LogToConsole(v, $"{DateTime.Now.ToString("HH:mm:ss")} {LogLevelDescription[v]}: {message}{(exception == null ? "" : $"\r\n异常信息:{exception.Message} {exception.InnerException?.Message}\r\n堆栈跟踪:{exception.StackTrace}")}"); if ((LogType & LogType.File) != 0) LogToFile(v, $"{DateTime.Now.ToString("HH:mm:ss")} {LogLevelDescription[v]}: {message}{(exception == null ? "" : $"\r\n异常信息:{exception.Message} {exception.InnerException?.Message}\r\n堆栈跟踪:{exception.StackTrace}")}\r\n"); LogEvent?.Invoke(v, message, exception); }
public static void Trace(string message, Exception exception = null) => Log(LogLevel.Trace, message, exception);
public static void Debug(string message, Exception exception = null) => Log(LogLevel.Debug, message, exception);
public static void Info(string message, Exception exception = null) => Log(LogLevel.Info, message, exception);
public static void Warn(string message, Exception exception = null) => Log(LogLevel.Warn, message, exception);
public static void Error(string message, Exception exception = null) => Log(LogLevel.Error, message, exception);
public static void Fatal(string message, Exception exception = null) => Log(LogLevel.Fatal, message, exception);
public delegate void LogAction(LogLevel v, string message, Exception exception = null);
public static event LogAction LogEvent; } }
|