::GetTickCount() C++运行时刻
System.Environment.TickCount; C#运行时刻
C++
CFile file;
CString strLog;
strLog.Empty();
file.Open("C:\\Xf4100.log", CFile::modeWrite | CFile::modeCreate | CFile::modeNoTruncate);
file.SeekToEnd();
file.Write(strLog, strLog.GetLength());
file.Close();
C#
public static void LogOut(string strLogTxt1, string strLogTxt2 = "", string strLogExe = "", string strFileName = "")
{
string strFilePath = System.Environment.CurrentDirectory;
System.Text.StringBuilder strLog = new System.Text.StringBuilder();
strLog.Append(DateTime.Now.ToString(@"yyyy-MM-dd hh:mm:ss fff "));
if (!string.IsNullOrEmpty(strLogExe))
{
strLog.Append(DateTime.Now.ToString(strLogExe + @" "));
}
strLog.Append(strLogTxt1);
if (!string.IsNullOrEmpty(strLogTxt2))
{
strLog.Append(@" " + strLogTxt2);
}
if (string.IsNullOrEmpty(strFileName))
{
strFileName = System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName + DateTime.Now.ToString("_MMdd") + @".log";
}
else
{
strFileName += @".log"; ;
}
strFilePath += @"\" + strFileName;
var fileStm = new System.IO.FileStream(strFilePath, System.IO.FileMode.Append, System.IO.FileAccess.Write);
var stmWiteer = new System.IO.StreamWriter(fileStm);
stmWiteer.WriteLine(strLog);
stmWiteer.Close();
}

本文对比展示了使用C++与C#两种不同语言实现日志记录的方法。C++部分通过CFile类和字符串操作来实现日志写入功能;而C#则利用StringBuilder进行日志内容构造,并采用FileStream及StreamWriter完成文件写入。两种实现均考虑了文件追加写入及日期时间戳的加入。
503

被折叠的 条评论
为什么被折叠?



