/* * Created by SharpDevelop. * User: careland * Date: 2015/1/29 * Time: 15:30 * * To change this template use Tools | Options | Coding | Edit Standard Headers. */ //#define IsConfig using System; using System.Diagnostics; using System.Runtime.CompilerServices; using System.Configuration; using System.Threading; using System.Reflection; namespace Log { /// <summary> /// Description of Log. /// </summary> public class Log { private static TraceSwitch ts; private Log() { } static Log() { init(); } private static bool IsError { get{return ts.TraceError;} } private static bool IsInfo { get{return ts.TraceInfo;} } private static bool IsDebug { get{return ts.TraceVerbose;} } private static bool IsWarn { get{return ts.TraceWarning;} } private static void WriteLine(String message,String category,String className) { String output=String.Format("{0} {1} {2} {3}",category,DateTime.Now,className,message); WriteLine(output); } private static void WriteLine(String message,String category) { WriteLine(message,category,GetCallClass()); } private static void WriteLine(String message) { Trace.WriteLine(message); } public static void Warn(String message) { if(IsWarn) WriteLine(message,"WARN "); } public static void Debug(String message) { if(IsDebug) WriteLine(message,"DEBUG"); } public static void Info(String message) { if(IsInfo) WriteLine(message,"INFO "); } public static void Error(String message) { if(IsError) WriteLine(message,"ERROR"); } public static void Warn(String message,object[] param) { Warn(getFormatMessage(message,param)); } public static void Debug(String message,object[] param) { Debug(getFormatMessage(message,param)); } public static void Info(String message,object[] param) { Info(getFormatMessage(message,param)); } public static void Error(String message,object[] param) { Error(getFormatMessage(message,param)); } public static void Warn(String message,Exception e) { if(IsWarn) { WriteLine(getExceptionMessage(message,e),"WARN ",GetCallClass(e)); } } public static void Debug(String message,Exception e) { if(IsDebug) { WriteLine(getExceptionMessage(message,e),"DEBUG",GetCallClass(e)); } } public static void Info(String message,Exception e) { if(IsInfo) { WriteLine(getExceptionMessage(message,e),"INFO ",GetCallClass(e)); } } public static void Error(String message,Exception e) { if(IsError) { WriteLine(getExceptionMessage(message,e),"ERROR",GetCallClass(e)); } } public static void Warn(String message,object[] param,Exception e) { Warn(getFormatMessage(message,param),e); } public static void Debug(String message,object[] param,Exception e) { Debug(getFormatMessage(message,param),e); } public static void Info(String message,object[] param,Exception e) { Info(getFormatMessage(message,param),e); } public static void Error(String message,object[] param,Exception e) { Error(getFormatMessage(message,param),e); } private static String getFormatMessage(String message,object[] param) { return String.Format(message,param); } private static String getExceptionMessage(String message,Exception e) { return String.Format("{0} [{1},{2}]",message,e.Message,e.StackTrace); } private static System.Threading.ThreadLocal<DateTime> local=new ThreadLocal<DateTime>(); public static void setTime() { local.Value=DateTime.Now; } public static long GetDiff() { if(local.IsValueCreated) { return (DateTime.Now-local.Value).Milliseconds; } return 0; } public static void WriteDiff() { Trace.WriteLine("the time diff:"+GetDiff()); } private static String SWITCHNAME="miniLogSwitch"; private static void init() { #if IsConfig ConfigurationSection section=GetConfigSection(); bool contain=false; if(section!=null) { SwitchAttribute[] attrs=SwitchAttribute.GetAll(Assembly.GetExecutingAssembly()); foreach(SwitchAttribute attr in attrs) { if(SWITCHNAME.Equals(attr.SwitchName)) { ts=new TraceSwitch(SwitchName,"default mini switch"); contain=true; break; } } } if(!contain) { ts=new TraceSwitch(SWITCHNAME,"default mini switch"); ts.Level=TraceLevel.Verbose;//默认输出全部 initListeners(); } #else ts=new TraceSwitch(SWITCHNAME,"default mini switch"); ts.Level=TraceLevel.Verbose;//默认输出全部 initListeners(); #endif } /// <summary> /// get top frame 获取调用者的当前类 /// from https://github.com/NLog/NLog/blob/master/src/NLog/LogFactory.cs /// </summary> /// <returns></returns> [MethodImpl(MethodImplOptions.NoInlining)] private static String GetCallClass(Exception e) { var start=DateTime.Now; if(e!=null) { return e.TargetSite.DeclaringType.FullName; } StackFrame frame=new StackFrame(4,false); String result=frame.GetMethod().DeclaringType.FullName; return result; } private static String GetCallClass() { return GetCallClass(null); } private static ConfigurationSection GetConfigSection() { return (ConfigurationSection)ConfigurationManager.GetSection("system.diagnostics"); } private static void initListeners() { TextWriterTraceListener text = new TextWriterTraceListener(System.AppDomain.CurrentDomain.BaseDirectory + "/log.txt"); text.TraceOutputOptions=TraceOptions.DateTime; Trace.Listeners.Add(text); } } }
/* * Created by SharpDevelop. * User: careland * Date: 2015/1/30 * Time: 16:11 * * To change this template use Tools | Options | Coding | Edit Standard Headers. */ using System; using System.Web; namespace Log.Test { /// <summary> /// test Log.class /// </summary> public class TestLog { private delegate void TestDele(); public TestLog() { } public void TestAsync() { TestDele del=new TestDele(TestDeleMethod); del.BeginInvoke(new AsyncCallback(TestCallBack),del); } public void TestDeleMethod() { var start=DateTime.Now; Log.Debug("TestDeleMethod"); var diff=(DateTime.Now-start).Milliseconds; Console.Error.WriteLine("diff1:"+diff); try { Object a=null; Object abc=a.Equals("b"); }catch(Exception e) { var start1=DateTime.Now; Log.Info("abc exception",e); var diff1=(DateTime.Now-start).Milliseconds; Console.Error.WriteLine("diff2:"+diff); } } public void TestCallBack(IAsyncResult result) { try { TestDele del=(TestDele)(result.AsyncState); del.EndInvoke(result); Log.Warn("call back"); }catch(Exception e) { Log.Warn("call back error",e); } } } }