| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- using System;
- using System.IO;
- using System.Text;
- using System.Web.UI;
- namespace Common
- {
- public class SysLog
- {
- private static Exception EX = null;
- private static string Leve = null;
- private static string Position = null;
- public static void WriteLog(Exception ex)
- {
- SysLog.EX = ex;
- SysLog.WriteDisk();
- }
- public static void WriteLog(Exception ex, string position)
- {
- SysLog.EX = ex;
- SysLog.Position = position;
- SysLog.WriteDisk();
- }
- public static void WriteLog(Exception ex, string position, string leve)
- {
- SysLog.EX = ex;
- SysLog.Leve = leve;
- SysLog.Position = position;
- SysLog.WriteDisk();
- }
- private static void WriteDisk()
- {
- FileStream fs = null;
- try
- {
- string str = "时间:" + DateTime.Now.ToString();
- if (SysLog.EX != null)
- {
- str = "错误消息:" + SysLog.EX.Message + "\t";
- if (SysLog.EX.HelpLink != null)
- {
- str = str + "与此关联的文件:" + SysLog.EX.HelpLink + "\t";
- }
- str = str + "错误消息字符串表示:" + SysLog.EX.StackTrace + "\t";
- }
- if (SysLog.Position != null)
- {
- str = str + "错误位置:" + SysLog.Position + "\t";
- }
- if (SysLog.Leve != null)
- {
- str = str + "错误级别:" + SysLog.Leve + "\t";
- }
- Page pag = new Page();
- str += "\r\n\r\n";
- string path = pag.Server.MapPath("/") + "\\Log\\" + DateTime.Now.ToShortDateString() + "Log.log";
- if (!File.Exists(path))
- {
- Directory.CreateDirectory(pag.Server.MapPath("/") + "\\Log");
- File.Create(path).Close();
- }
- byte[] bytData = Encoding.Default.GetBytes(str);
- fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite, 2048);
- fs.Write(bytData, 0, bytData.Length);
- }
- finally
- {
- if (fs != null)
- {
- fs.Close();
- }
- SysLog.EX = null;
- SysLog.Position = null;
- SysLog.Leve = null;
- }
- }
- }
- }
|