/// <summary>
/// 记录日志
/// 以日期为文件名,一天一个文件名,每条记录为一个日志
/// 格式:时间yyyy-MM-dd HH:mm:ss(24h)+空格+日志主体信息
/// 如:2010-08-08 10:10:10 访问作业系统失败,请检查作业系统是否正常运作。
/// </summary>
/// <param name="strLogPath">输入:日志文件存放路径</param>
/// <param name="strMsgInfo">输入:日志信息主体信息</param>
internal static void WriteLog(string strLogPath, string strMsgInfo)
{
try
{
//判断路径是否存在
DirectoryInfo dirFolderPath = new DirectoryInfo(strLogPath);
if (!dirFolderPath.Exists) dirFolderPath.Create();
string strTodayDate = DateTime.Now.ToString("yyyy-MM-dd");//当前日期
string strFileName = dirFolderPath + @"\" + strTodayDate + ".log";
FileStream fs = null;
if (File.Exists(strFileName))
fs = new FileStream(strFileName, FileMode.Append, FileAccess.Write);
else
fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write);
strMsgInfo = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + strMsgInfo + "\n\r";//加上换行符号
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(strMsgInfo);
sw.Flush();
sw.Close();
fs.Close();
}
catch (Exception ex)
{
CLog.WriteLog("c:\\Log\\LogWriteError", ex.Message + Environment.NewLine + "原始消息:" + Environment.NewLine + strMsgInfo);
}
}
调用:WriteLog("c:\\log", ex.Message);