日期:2014-05-18 浏览次数:20971 次
/* * 异步日志类 * * * */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.Data.SqlClient; using System.IO; namespace Lib.Common.Common { public sealed class AsynLogManager { #region Member Variables /// <summary> /// 用于Trace的组织输出的类别名称 /// </summary> private const string trace_sql = "\r\n***********************TRACE_SQL {0}*****************************\r\nTRACE_SQL"; /// <summary> /// 用于Trace的组织输出的类别名称 /// </summary> private const string trace_exception = "\r\n***********************TRACE_EXCEPTION {0}***********************"; /// <summary> /// 当前日志的日期 /// </summary> private static DateTime CurrentLogFileDate = DateTime.Now; /// <summary> /// 日志对象 /// </summary> private static TextWriterTraceListener twtl=new TextWriterTraceListener(); /// <summary> /// 日志根目录 /// </summary> private static string log_root_directory =AppDomain.CurrentDomain.BaseDirectory; //@"D:\log"; /// <summary> /// 日志子目录 /// </summary> private static string log_subdir; /// <summary> /// " {0} = {1}" /// </summary> private const string FORMAT_TRACE_PARAM = " {0} = {1}"; /// <summary> /// 1 仅控制台输出 /// 2 仅日志输出 /// 3 控制台+日志输出 /// </summary> private static readonly int flag = 2; //可以修改成从配置文件读取 #endregion #region Constructor static AsynLogManager() { System.Diagnostics.Trace.AutoFlush = true; switch (flag) { case 1: System.Diagnostics.Trace.Listeners.Add(new ConsoleTraceListener()); break; case 2: System.Diagnostics.Trace.Listeners.Add(TWTL); break; case 3: System.Diagnostics.Trace.Listeners.Add(new ConsoleTraceListener()); System.Diagnostics.Trace.Listeners.Add(TWTL); break; } } #endregion #region Method #region trace /// <summary> /// 异步错误日志 /// </summary> /// <param name="value"></param> public static void Trace(Exception ex) { new AsyncLogException(BeginTraceError).BeginInvoke(ex, null, null); } /// <summary> /// 异步SQL日志 /// </summary> /// <param name="cmd"></param> public static void Trace(SqlCommand cmd) { new AsyncLogSqlCommand(BeginTraceSqlCommand).BeginInvoke(cmd, null, null); } /// <summary> /// 异步SQL日志 /// </summary> /// <param name="sql"></param> /// <param name="parameter"></param> public static void Trace(string sql, params SqlParameter[] parameter) { new AsyncLogSql(BeginTraceSql).BeginInvoke(sql, parameter, null, null); } #endregion #region delegate private delegate void AsyncLogException(Exception ex); private delegate void AsyncLogSqlCommand(SqlCommand cmd); private delegate void AsyncLogSql(string sql, params SqlParameter[] parameter); private static void BeginTraceErro