程序代码记Log
private static object s_lock = new object(); public static void TraceLog(string message, string logFileName, string logPath = null) { string tmppath = string.Empty; if (logPath != null && logPath.Length > 0) tmppath = logPath; else tmppath = AppDomain.CurrentDomain.BaseDirectory + "\\Log\\"; if (!Directory.Exists(tmppath)) { Directory.CreateDirectory(tmppath); } lock (s_lock) { StreamWriter myFile = null; try { myFile = new StreamWriter(tmppath + DateTime.Now.ToString("yyyyMMdd") + logFileName.ToString() + ".log", true, Encoding.Default); TextWriterTraceListener textL = new TextWriterTraceListener(myFile); DefaultTraceListener defL = new DefaultTraceListener(); if (Trace.Listeners.IndexOf(textL) == -1) { Trace.Listeners.Clear(); Trace.Listeners.Add(textL); Trace.Listeners.Add(defL); } Trace.AutoFlush = true; Trace.WriteLine(string.Empty); Trace.WriteLine(message); } catch (Exception ioe) { Console.WriteLine(ioe.Message); } finally { if (myFile != null) { myFile.Close(); myFile.Dispose(); } } } }
如果您觉得本文对你有用,不妨帮忙点个赞,或者在评论里给我一句赞美,小小成就都是今后继续为大家编写优质文章的动力!
欢迎您持续关注我的博客:)
版权所有,欢迎保留原文链接进行转载:)