public class WriteLog
{
public static void WriteLogInfo(SqlException ex,string path)
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path += "\\" + DateTime.Now.ToShortDateString() + ".log";
WriteLogInfo(ex, path);
}
public static void WriteLogInfo(Exception ex,string path)
{
using(StreamWriter sw=new StreamWriter(path, true, Encoding.Default))
{
sw.WriteLine("***[" + DateTime.Now.ToShortDateString() + "]***");
if (ex != null)
{
sw.Write(ex.Message);
}
else
{
sw.WriteLine("Exception is null");
}
sw.WriteLine();
}
}
/// <summary>
/// 创建日志文件
/// </summary>
/// <param name="ex"></param>
public static void CreateLog(Exception ex)
{
//string AppName=Assembly.GetExecutingAssembly().GetName().CodeBase();
//string path=Path.GetDirectoryName(AppName)+"\\log";
string path = Application.StartupPath + "\\log";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);//创建日志文件夹
path += "\\" + DateTime.Now.ToShortDateString() + ".log";
WriteLogInfo(ex, path);
}
}
public static void CreateLog(SqlException ex)
{
string path = Application.StartupPath + "\\log";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path += "\\" + DateTime.Now.ToShortDateString() + ".log";
WriteLogInfo(ex, path);
}
}