c# 写入log 自己最常用的调试代码,可以适当 lock 【备用】非 log4

时间:2020-06-17 10:33:28   收藏:0   阅读:76
        public static string WriteLogFileName(string strLog, string strFileName = "MyLog")
        {
            string strPath = System.Windows.Forms.Application.StartupPath + @"\Log\";
            string strLogPathName = strPath + string.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now).Substring(0, 9) + strFileName + ".log";
            //lock (lockerMemoLog)
            {
                try
                {
                    if (!Directory.Exists(strPath))
                    {
                        Directory.CreateDirectory(strPath);
                    }
                    string strLogMessage = string.Empty;
                    StreamWriter swLog;
                    {
                        strLogMessage = string.Format("{0}:#{1}", DateTime.Now, (strLog));
                    }
                    if (!File.Exists(strLogPathName))
                    {
                        swLog = new StreamWriter(strLogPathName);
                    }
                    else
                    {
                        swLog = File.AppendText(strLogPathName);
                    }
                    swLog.WriteLine(strLogMessage);
                    swLog.Close();
                    return "0";
                }
                catch (Exception ex)
                {
                    return ex.ToString();
                }
            }
        }

 

原文:https://www.cnblogs.com/2eggs/p/13150356.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!