c # Small function: to write the log

When debugging a program, or always use to log, so you can easily operate the logger. This method is based on the number of days, every day will generate a log file.

Log code is as follows:

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();
            }
        }

Then you can call this method to write the log. (PubLibrary class is WriteErrorlog Method)

PubLibrary.WriteErrorlog ( " This is a log " );

 

Guess you like

Origin www.cnblogs.com/masha2017/p/11123238.html