context: using RsWork.Universal.Template1;
  using System;
  using System.IO;
  using System.Text;
  using System.Windows;
  using System.Collections;
  using System.Collections.Generic;
  
  namespace RsWork.Universal
  {
      public enum LogType{
  		Info,Warning,Error,UserInput,AppOutput,Crash
  	}
  	public static class Logger
  	{
          private static int ExceptionIndex=0;
          private static string[] ExceptionS=new string[30];
          public static string[] Exceptions { get{ return ExceptionS; } }
          private static bool IsInited=false;
          private static string patH=@"E:\Log.Log";
          public static string path { get { return patH; } }
          private static StringBuilder logs=new StringBuilder("");
          public static StringBuilder Logs { get { return logs; } }
  		//private static FileStream fs;
  		//private static StreamWriter sw;
  		public static StringBuilder DefaultHost=new StringBuilder("Log Service");
  		public static bool IsPrintOutDateTime=true;
  		public static StringBuilder DefaultMessage=new StringBuilder("[TestMessage]");
  		public static bool LogToFile=true;
  		public static void Init(string path,string Defaulthost,bool IsPrintOutDatetime,string defaultmessage)
  		{
              FileStream fs;
              StreamWriter sw;
              patH = path;
              DefaultHost = new StringBuilder( Defaulthost);
              IsPrintOutDateTime = IsPrintOutDatetime;
              DefaultMessage = new StringBuilder(defaultmessage);
  			if(File.Exists(path))
  			{
                  try
                  {
                      fs = new FileStream(path, FileMode.Create);
  					sw=new StreamWriter(fs);
  					Logger.WriteLine("==============="/*((App)Application.Current).ApplicationInfo.AppName*/+" Start Log "+"@"+DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss")+"===============");
                      fs.Close();
                  }
                  catch(Exception e)
                  {
                      MessageBox.Show(FormatException(e));
  
  
                  }
  				
  			}
  			IsInited=true;
  			
  		}		
          //private static StreamWriter ReInit()
          //{
             
          //    return sw;
          //}
          //private static void Close(FileStream fs,StreamWriter sw)
          //{
          //    sw.Close();
          //    fs.Close();
  
          //}
  		private static string CollectInfo(string message=null,string host=null,bool IsPrintOutDateTime=true,string PreMessage=null)
  		{
              StringBuilder sb;
              if (!IsInited)
              {
                  return null;
              }
              if (message != null)
              {
                 sb = new StringBuilder(message);
              }
              else
              {
                  if (DefaultMessage != null)
                      sb = DefaultMessage;
                  else
                      sb = new StringBuilder("");
              }
  			if(host!=null)
  				sb.Insert(0,String.Format("[{0}]",host));
  			else
  			{
  				if(DefaultHost!=null)
  					sb.Insert(0,String.Format("[{0}]",DefaultHost));
  			}
  			if(PreMessage!=null)
  				sb.Insert(0,PreMessage);
  			if(!IsPrintOutDateTime)
  				sb.Insert(0,String.Format("[{0}]",DateTime.Now.ToString("hh:mm:ss")));
  
              return sb.ToString();
  		}
  		public static void Write(string s)
  		{
  			if(IsInited)
  			{
                  FileStream fs=null;
                  StreamWriter sw = null;
                  try
                  {
                      fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
                      sw = new StreamWriter(fs);
                      sw.Write(s);
                      
                  }
                  catch (Exception E)
                  {
                      MessageBox.Show(FormatException(E));
                  }
                  finally
                  {
                      if(sw!=null&&fs!=null)
                      {
                          sw.Close();
                          fs.Close();
                      }
                      
                  }
  			}
  			logs.Append(s);
  		}
  		public static void WriteLine(string s)
  		{
              if (IsInited)
              {
                  FileStream fs = null;
                  StreamWriter sw = null;
                  try
                  {
                      fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
                      sw = new StreamWriter(fs);
                      sw.WriteLine(s);
                      
                  }
                  catch (Exception E)
                  {
                      MessageBox.Show(FormatException(E));
                      
                  }
                  finally
                  {
                      if (sw != null && fs != null)
                      {
                          sw.Close();
                          fs.Close();
                      }
                  }
              }
              
              logs.Append("\r\n"+s);
  		}
  		public static void StopLog()
  		{
  			if(!IsInited)
  			{
  				Logger.WriteLine("==============="+((App)Application.Current).ApplicationInfo.AppName+" Stop Log "+"@"+DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss")+"===============");
  				
  				
  			}
  		}
          public static void Log(Exception e)
          {
              string s=FormatException(e);
              ExceptionS[ExceptionIndex] = s;
              ExceptionIndex++;
              WriteLine(s);
          }
  		public static void Log(string message=null,string host=null,string PreMessage=null,bool IsPrintOutDateTime=true)
  		{
  			Logger.WriteLine(CollectInfo(message,host,IsPrintOutDateTime, PreMessage));
  		}
  		public static void Log(LogType logtype,string message=null,string host=null,string PreMessage=null,bool IsPrintOutDateTime=true)
  		{
              string s=CollectInfo(message, host, IsPrintOutDateTime, PreMessage);
              s = InsertSign(s, logtype);
              Logger.WriteLine(s);
          }
          public static string FormatSingleException(Exception e,bool IsInner=false)
          {
              StringBuilder sb = new StringBuilder();
              if (!IsInner)
              {
                  sb.AppendLine("--------Application Has Found An Exception at "+DateTime.Now.ToString("yyyy.MM.dd hh:mm:ss")+"--------");
              }
              else
                  sb.AppendLine("======Inner Exception======");
              sb.AppendLine("ToString():"+e.ToString());
              sb.AppendLine("Source:"+e.Source);
              sb.AppendLine("Message:"+e.Message);
              sb.AppendLine("Error Method:"+e.TargetSite);
              sb.AppendLine("StackTrace:"+e.StackTrace);
              sb.AppendLine("HelpLink:"+e.HelpLink);
              foreach(DictionaryEntry data in e.Data)
              {
                  sb.AppendLine(string.Format("\tKey:{0}\tValue:{1}", data.Key, data.Value));
              }
              return sb.ToString();
          }
  	    public static string FormatException(Exception e)
          {
  
gt:             StringBuilder sb = new StringBuilder(FormatSingleException(e));