///-----------------------------------------------------------------------------
/// <summary>创建错误日志 在c:\ErrorLog\</summary>
/// <param name="strFunctionName">strFunctionName,调用方法名</param>
/// <param name="strErrorNum">strErrorNum,错误号</param>
/// <param name="strErrorDescription">strErrorDescription,错误内容</param>
/// <returns></returns>
/// <history>2009-05-29 Created</history>
///-----------------------------------------------------------------------------
//举例:
// try
// { 要监视的代码 }
// catch()
// { myErrorLog.m_CreateErrorLogTxt("myExecute(" & str执行SQL语句 & ")", Err.Number.ToString, Err.Description) }
// finally
// { }
public static void m_CreateErrorLogTxt(string strFunctionName, string strErrorNum, string strErrorDescription)
{
string strMatter; //错误内容
string strPath; //错误文件的路径
DateTime dt = DateTime.Now;
try
{
//Server.MapPath("./") + "File"; 服务器端路径
strPath = Directory.GetCurrentDirectory() + "\\ErrorLog"; //winform工程\bin\目录下 创建日志文件夹
//strPath = "c:" + "\\ErrorLog";//暂时放在c:下
if(Directory.Exists(strPath)==false) //工程目录下 Log目录 '目录是否存在,为true则没有此目录
{
Directory.CreateDirectory(strPath); //建立目录 Directory为目录对象
}
strPath = strPath + "\\" + dt.ToString("yyyyMM");
if(Directory.Exists(strPath) == false) //目录是否存在 '工程目录下 Log\月 目录 yyyymm
{
Directory.CreateDirectory(strPath); //建立目录//日志文件,以 日 命名
}
strPath = strPath + "\\" + dt.ToString("dd") + ".txt";
strMatter = strFunctionName + " , " + strErrorNum + " , " + strErrorDescription;//生成错误信息
StreamWriter FileWriter= new StreamWriter(strPath, true); //创建日志文件
FileWriter.WriteLine("Time: " + dt.ToString("o") + " Err: " + strMatter);
FileWriter.Close(); //关闭StreamWriter对象
}
catch(Exception ex)
{
//("写错误日志时出现问题,请与管理员联系! 原错误:" + strMatter + "写日志错误:" + ex.Message.ToString());
string str=ex.Message.ToString();
}
}
/// <summary>
/// 创建目录
/// </summary>
/// <param name="DirInfo"></param>
/// <returns></returns>
private static bool CreateDirectory(string DirInfo)
{
try
{
if (!Directory.Exists(DirInfo))
Directory.CreateDirectory(DirInfo);
return true;
}
catch { }
return false;
}
/// <summary>
/// 创建文件
/// </summary>
/// <param name="FilePath"></param>
/// <returns></returns>
private static bool CreatFile(string FilePath)
{
try
{
if (!File.Exists(FilePath))
{
FileStream fs = File.Create(FilePath);
fs.Close();
}
return true;
}
catch { }
return false;
}
/// <summary>
/// 创建日志文件
/// </summary>
/// <param name="logType"></param>
/// <param name="msDate"></param>
/// <returns></returns>
private static bool CreatFile(LogType logType)
{
try
{
switch (logType)
{
case LogType.System:
CreatFile(SystemLogPath);
break;
case LogType.User:
string userDir = string.Format("{0}\\Log\\Users\\{1}", Application.StartupPath, UserInfo);
if (!Exists(userDir, false))//不存在用户目录创建
CreateDirectory(userDir);
CreatFile(UserLogPath);
break;
}
return true;
}
catch { }
return false;
}
/// <summary>
/// 写入信息到文件
/// </summary>
/// <param name="msPath"></param>
/// <param name="msMessage"></param>
private static void WriteText(string msPath, string msMessage)
{
try
{
FileStream fs = new FileStream(msPath, System.IO.FileMode.Append, System.IO.FileAccess.Write, FileShare.Write);
System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.Default);
sw.WriteLine(DateTime.Now.ToString());
sw.WriteLine(msMessage);
sw.Close();
fs.Close();
}
catch (IOException ex)
{ }
catch (Exception ex)
{ }
finally
{ }
}
/// <summary>
/// 写入用户日志
/// </summary>
/// <param name="msMessage"></param>
public static void WriteUserLog(string msMessage)
{
ConstructUserPath();
if (!Exists(LogType.User))
{
CreatFile(LogType.User);
}
//WriteText(UserLogPath, System.DateTime.Now.ToString("yyyy-MM-dd"));
WriteText(UserLogPath, msMessage);
}
/// <summary>
/// 写入系统日志
/// </summary>
/// <param name="msMessage"></param>
public static void WriteSystemLog(string msMessage)
{
ConstructSystemPath();
if (!Exists(LogType.System))
{
CreatFile(LogType.System);
}
WriteText(SystemLogPath, msMessage);
}
/// <summary>
/// 写入系统日志
/// </summary>
/// <param name="msMessage"></param>
public static void WriteSystemLogFormat(string msMessage)
{
ConstructSystemPath();
if (!Exists(LogType.System))
{
CreatFile(LogType.System);
}
// WriteText(SystemLogPath, string.Format("==========================================================================\r\nDateTime:{0}", DateTime.Now.ToString()));
WriteText(SystemLogPath, string.Format("Description:{0}", msMessage));
}
public static void WriteLog(string msMessage, string path)
{
}
/// <summary>
/// 构造路径
/// </summary>
private static void ConstructUserPath()
{
if (UserInfo == string.Empty)
throw new Exception("用户名称不能为空");
if (UserLogPath == string.Empty)
UserLogPath = string.Format("{0}\\Log\\Users\\{1}\\{2}.txt", Application.StartupPath, UserInfo, DateTime.Now.ToString("yyyy-MM-dd"));
}
/// <summary>
/// 构造系统日志路径
/// </summary>
private static void ConstructSystemPath()
{
if (SystemLogPath == string.Empty)
{
SystemLogPath = string.Format("{0}\\Log\\System\\{1}.txt", Application.StartupPath, DateTime.Now.ToString("yyyy-MM-dd"));
}
string strPath = SystemLogPath.Substring(0, SystemLogPath.LastIndexOf("\\"));
if (!System.IO.Directory.Exists(strPath))
{
System.IO.Directory.CreateDirectory(strPath);
}
}
/// <summary>
/// 创建目录
/// </summary>
/// <param name="mp_dir"></param>
/// <returns></returns>
private static bool ConstructDirectory(string mp_dir)
{
//if(string.Empty(mp_dir))
// return false;
//if (!System.IO.Directory.Exists(mp_dir))
//{
// DirectoryInfo info = System.IO.Directory.CreateDirectory(mp_dir);
// return info.Exists;
//}
return false;
}
这里收集了用户,系统,错误这三种日志的写法,直接可以拿来用,虽然没有技术含量,但是复制,粘贴,可以有效的减少工作量