C# 手寫簡單實現日誌輸出

 在沒有引入log4j.dll 的情況下,簡單輸出日誌: 

public static void ErrorLog(string mssg)
        {
            string FilePath = "F:/ErrorLog.txt";
            StreamWriter tw = null;
            try
            {
                if (!File.Exists(FilePath))
                {
                    File.Create(FilePath);
                }
                if (System.IO.File.Exists(FilePath))
                {
                    using (tw = System.IO.File.AppendText(FilePath))
                    {
                        tw.WriteLine(DateTime.Now.ToString() + "> " + mssg);
                        tw.Flush();
                    }
                }
                else
                {
                    tw = new StreamWriter(FilePath);
                    tw.WriteLine(DateTime.Now.ToString() + "> " + mssg);
                }
            }
            catch (Exception ex)
            { }
            finally
            {
                if (tw != null)
                    tw.Close();
            }
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章