程序运行或者调试的时候总是要用到日志,这样可以方便记录程序的操作。这个方法是根据天数,每一天都会生成一个日志文件。
日志代码如下:
public static void WriteErrLog(string strErrCode) { string strLogPath = "D:\\日志"; string strFileName = strLogPath + "\\Log" + System.DateTime.Now.ToString("yyyyMMdd") + ".txt"; if (!Directory.Exists(strLogPath)) { Directory.CreateDirectory(strLogPath); } StreamWriter sw = File.AppendText(strFileName); try { sw.WriteLine(System.DateTime.Now.ToString() + "," + strErrCode); sw.Flush(); } catch { return; } finally { sw.Close(); } }
然后就可以直接调用这个方法来写日志了。(PubLibrary是WriteErrorlog方法类)
PubLibrary.WriteErrorlog("这是日志");