用文字檔案紀錄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.
if (string.IsNullOrEmpty(
{
FilePath = @"C:\\ICM_BIN\\default\\";
}
//File Path
string filename = FilePath + string.Format("\\{0:yyyyMMdd}.
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(
}
}