网上找的比较详细的可用的日志
/// <summary>
/// 日志文件
/// </summary>
/// <param name="message">日志信息</param>
/// <param name="title">日志标题</param>
public static void CreateLog(string message, string title)
{
string path = Directory.GetCurrentDirectory() + "\\LoginfoMsg";
string filename = path + "\\MsgLog.txt";
string cont = "";
FileInfo fileInf = new FileInfo(filename);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
if (File.Exists(filename))//如何文件存在 则在文件后面累加
{
FileStream myFss = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
StreamReader r = new StreamReader(myFss);
cont = r.ReadToEnd();
r.Close();
myFss.Close();
}
FileStream myFs = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
StreamWriter n = new StreamWriter(myFs);
n.WriteLine(cont);
n.WriteLine("------------------------------------------------------Begin-------------------------------------------------");
n.WriteLine("*****" + title + "*****");
n.WriteLine("时间:" + DateTime.Now.ToString());
n.WriteLine("信息:" + message);
n.WriteLine("-------------------------------------------------------end--------------------------------------------------");
n.Close();
myFs.Close();
if (fileInf.Length >= 1024 * 1024 * 200)
{
string NewName = path + "MsgLog" + DateTime.Now.ToShortDateString() + ".txt";
File.Move(filename, NewName);
}
}
/// <summary>
/// 异常日志
/// </summary>
public static void WriteLogInfo(Exception ex)
{
string path = Directory.GetCurrentDirectory() + "\\LoginfoEx";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path += "\\" + DateTime.Now.ToShortDateString() + ".log";
using (StreamWriter sw = new StreamWriter(path, true, Encoding.Default))
{
sw.WriteLine("*************************************************["
+ DateTime.Now.ToShortDateString()
+ "]**********************************************");
if (ex != null)
{
sw.WriteLine("[ErrorType]" + ex.GetType());
sw.WriteLine("[TargeSite]" + ex.TargetSite);
sw.WriteLine("[Message]" + ex.Message);
sw.WriteLine("[Source]" + ex.Source);
sw.WriteLine("[StackTrace]" + ex.StackTrace);
}
else
{
sw.WriteLine("Exception is NULL");
}
sw.WriteLine();
}
}