用文字檔案紀錄Log

Sample Code:

StreamWriter file = new StreamWriter(@"C:\inetpub\wwwroot\ECN_OS\UploadFile\log123.txt", true);
file.WriteLine("openConnection: " + DateTime.Now.ToString());
file.Close();

 

 

===== 2016.12.1 =====

static void WriteToLog(string strMsg)
    {
        string sPath = HttpContext.Current.Server.MapPath(string.Empty) + @"\LogFiles\Log_" + DateTime.Now.ToString("MM-dd-yyyy") + ".txt";
        //string sPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase+ @"LogFiles\Log_" + DateTime.Now.ToString("MM-dd-yyyy") + ".log";

        FileStream fsLog = new FileStream(sPath, FileMode.Append, FileAccess.Write, FileShare.Write);
        StreamWriter swLog = new StreamWriter(fsLog);
        swLog.Write(DateTime.Now.ToString() + " ");
        swLog.WriteLine(strMsg);
        swLog.Flush();
        swLog.Close();
    }

 

 

===== 2017.07.26 =====

private static void ErrorWrite(string message)

    {

        FilePath = WebConfigurationManager.AppSettings["FilePath"];

        if (string.IsNullOrEmpty(FilePath))

       {

            FilePath = @"C:\\ICM_BIN\\default\\";

        }

        //File Path

        string filename = FilePath + string.Format("\\{0:yyyyMMdd}.txt", DateTime.Now);

        FileInfo finfo = new FileInfo(filename);

        if (finfo.Directory.Exists == false)

        {

            finfo.Directory.Create();

        }

        //File Content

        string writeString = string.Format("{0:yyyy/MM/dd HH:mm:ss} {1}", DateTime.Now, message) + Environment.NewLine;

 

        lock (thisLock)

        {

            File.AppendAllText(filename, writeString, Encoding.Unicode);

        }

    }

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 sendohlun 的頭像
    sendohlun

    廣度與深度

    sendohlun 發表在 痞客邦 留言(0) 人氣()