/// <summary> /// Logwriters the specified source. /// </summary> /// <param name="source">object that wrote the logentry.</param> /// <param name="prio">Importance of the log message</param> /// <param name="message">The message.</param> public void Write(object source, LogPrio prio, string message) { StringBuilder sb = new StringBuilder(); sb.Append(DateTime.Now.ToString()); sb.Append(" "); sb.Append(prio.ToString().PadRight(10)); sb.Append(" | "); #if DEBUG StackTrace trace = new StackTrace(); StackFrame[] frames = trace.GetFrames(); int endFrame = frames.Length > 4 ? 4 : frames.Length; int startFrame = frames.Length > 0 ? 1 : 0; for (int i = startFrame; i < endFrame; ++i) { sb.Append(frames[i].GetMethod().Name); sb.Append(" -> "); } #else sb.Append(System.Reflection.MethodBase.GetCurrentMethod().Name); sb.Append(" | "); #endif sb.Append(message); Console.ForegroundColor = GetColor(prio); Console.WriteLine(sb.ToString()); Console.ForegroundColor = ConsoleColor.Gray; }
/// <summary> /// 写日志消息,实现接口 /// </summary> public void Write(object source, LogPrio prio, string message) { StringBuilder sb = new StringBuilder(); sb.Append(DateTime.Now.ToString()); sb.Append(" "); sb.Append(prio.ToString().PadRight(5)); sb.Append(" ("); #if DEBUG StackTrace trace = new StackTrace(); StackFrame[] frames = trace.GetFrames(); int endFrame = frames.Length > 4 ? 4 : frames.Length; int startFrame = frames.Length > 0 ? 1 : 0; for (int i = startFrame; i < endFrame - 1; ++i) { sb.Append(frames[i].GetMethod().Name); sb.Append("."); } sb.Append(frames[endFrame - 1].GetMethod().Name); sb.Append(") "); #else sb.Append(System.Reflection.MethodBase.GetCurrentMethod().Name); sb.Append(" | "); #endif sb.Append(message); Console.ForegroundColor = GetColor(prio); Console.WriteLine(sb.ToString()); Console.ForegroundColor = ConsoleColor.Gray; //将日志写入目标文件 string logPath = @"Log\log.txt"; //文件相对路径 if (System.IO.File.Exists(logPath)) { System.IO.StreamWriter sw = new System.IO.StreamWriter(logPath, true, Encoding.UTF8); sw.WriteLine(sb); sw.Close(); } }
public void Write(object source, LogPrio priority, string message) { traceLog(priority.ToString(), message); }
public static void Log(string msg, LogPrio prio = LogPrio.Info) { Console.WriteLine(prio.ToString() + " : " + msg); }