C#很多异步机制使程序无法使用断点调试,这时候我们就需要使用日志输出。使用log4net一定要先引用net4log这个dll,不然无法使用。
作者很懒,直接上代码吧。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using log4net; namespace HacFin.SuperCapture.Switcher { public static class LogHelper { private static bool IsLogOpen = true; public static ILog log = log4net.LogManager.GetLogger("HKFY_"); public static void LogUserAction(this Object sender, String msg) { if (!IsLogOpen) return; LogDebug(sender, msg, "UserAction"); } public static void LogMeasureIn(this Object sender, String msg) { if (!IsLogOpen) return; LogInfo(sender, msg, "Measure_In"); } public static void LogMeasureOut(this Object sender, String msg) { if (!IsLogOpen) return; LogInfo(sender, msg, "Measure_Out"); } public static void LogDebug(this Object sender, String msg = "", String title = "") { if (!IsLogOpen) return; String txt = title + " - " + msg; log.Debug(txt); } public static void LogWarn(this Object sender, String msg = "", String title = "") { if (!IsLogOpen) return; String txt = title + " - " + msg; log.Warn(txt); } #region - 致命错误不受开关控制,一直需要记录 public static void LogError(this Object sender, String msg = "", String title = "") { String txt = title + " - " + msg; log.Error(txt); } public static void LogFatal(this Object sender, String msg = "", String title = "") { String txt = title + " - " + msg; log.Fatal(txt); } #endregion public static void LogInfo(this Object sender, String msg = "", String title = "") { if (!IsLogOpen) return; String txt = title + " - " + msg; log.Info(txt); } public static String ToFullLogInfo(this Exception ex) { if (ex == null) return String.Empty; String exMsg = ex.Message; if (ex.Source != null) exMsg += Environment.NewLine + ex.Source; if (ex.StackTrace != null) exMsg += Environment.NewLine + ex.StackTrace; if (ex.InnerException != null) { exMsg += Environment.NewLine + "InnerException : " + ex.InnerException.Message; if (ex.InnerException.Source != null) exMsg += Environment.NewLine + ex.InnerException.Source; if (ex.InnerException.StackTrace != null) exMsg += Environment.NewLine + ex.InnerException.StackTrace; } return exMsg; } } }
原文:http://www.cnblogs.com/microstep/p/4949959.html