Log记录日志
using System;
using System.IO;
using System.Collections;
using System.Collections.Specialized;
using System.Web;
namespace AppUtility
{
/// <summary>
/// LogWriter 的摘要说明。
/// </summary>
public class TextLogWriter : IDisposable
{
private TextLogWriter()
{
AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}
void CurrentDomain_ProcessExit(object sender, EventArgs e)
{
Flush();
}
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Flush();
}
void Flush()
{
if (_writer != null)
{
_writer.Flush();
_writer.Dispose();
_writer = null;
}
}
void CurrentDomain_DomainUnload(object sender, EventArgs e)
{
Flush();
}
static private TextLogWriter _instance = null;
static private Hashtable _instanceTable = Hashtable.Synchronized(new Hashtable());
static public TextLogWriter Instance()
{
if (_instance == null) _instance = NamedInstance("LOG");
return _instance;
}
static public TextLogWriter NamedInstance(string logDir)
{
logDir = ParseDir(logDir);
if (_instanceTable.ContainsKey(logDir)) return (TextLogWriter)_instanceTable[logDir];
TextLogWriter instance = new TextLogWriter();
instance.Initial(logDir);
if (!_instanceTable.ContainsKey(logDir)) _instanceTable.Add(logDir, instance);
return instance;
}
static private string ParseDir(string logDir)
{
if (logDir.IndexOf(':') > -1)
{
return logDir;
}
return AppDomain.CurrentDomain.BaseDirectory + (logDir.StartsWith("\\") ? "" : "\\") + logDir;
}
private string _fileDir;
public virtual void Initial(string configLogFileDir)
{
_fileDir = configLogFileDir;
if (!Directory.Exists(_fileDir)) Directory.CreateDirectory(_fileDir);
}
private StreamWriter _writer;
private string _logPath;
private StreamWriter NewWriter(string filePath)
{
_logPath = filePath;
return new StreamWriter(filePath, true, System.Text.Encoding.UTF8);
}
public virtual void Write(string message)
{
message = "--------------------------------------------"
+ LogBaseInfo() + message
+ "\r\n-------------------------------------------";
try
{
if (_writer == null)
{
string filePath = System.IO.Path.Combine(_fileDir, DateTime.Now.ToString("yyyyMMdd") + ".log");
_writer = NewWriter(filePath);
}
else
{
string filePath = System.IO.Path.Combine(_fileDir, DateTime.Now.ToString("yyyyMMdd") + ".log");
if (filePath != _logPath)
{
_writer.Flush();
_writer.Close();
_writer = NewWriter(filePath);
_logPath = filePath;
}
}
_writer.Write(message);
}
catch { ;/*do nothing*/}
finally
{
// if (writer != null)
// {
// writer.Close();
// }
}
}
public virtual void Write(Exception ex)
{
Write(ExceptionLog(ex));
}
private string LogBaseInfo()
{
return "\r\n时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n";
}
private string ExceptionLog(Exception ex)
{
string log = ex.Message + "\r\n"
+ ex.StackTrace + "\r\n";
while (ex.InnerException != null)
{
ex = ex.InnerException;
log += "\t" + ex.Message + "\r\n"
+ ex.StackTrace;
}
if (HttpContext.Current != null)
{
log += Environment.NewLine + "Url:" + HttpContext.Current.Request.Url.ToString();
if (HttpContext.Current.Request.RawUrl != HttpContext.Current.Request.Url.ToString())
{
log += Environment.NewLine + "RawUrl:" + HttpContext.Current.Request.RawUrl;
}
}
return log;
}
#region IDisposable Members
public void Dispose()
{
if (_writer != null)
{
try
{
Flush();
_writer = null;
}
catch { }
}
}
#endregion
}
}
备注:调用时候TextLogWriter.Instance().Write(ex)即可