日志文件的生成

  开发系统过程中,大部分都要用到写日志来跟踪具体的调用信息,或可能出错时,方便查找出错的
具体地方.目前关于日志的第三方控件非常的多,用的比较多的好象是log4net.在这我不介绍这个控件,而是用系统的STACKTRACE来跟踪.CODE AS FOLLOW:
  public static  void WriteLogFileInfo()
  {   
   StreamWriter sw =new StreamWriter(( System.IO.Path.Combine(System.Environment.CurrentDirectory,System.DateTime.Now.ToString("yyyyMMdd hhmmss")+"_log" +".log")));
   m_oSt = new StackTrace(true);
   //get the prior ten frames
   for(int iCnt = 0;iCnt < m_oSt.FrameCount && iCnt <10;iCnt++)
   {
    m_sLogDetailInfo = m_oSt.GetFrame(iCnt).GetMethod().DeclaringType.FullName +":" + m_oSt.GetFrame(iCnt).GetMethod().Name;  
 
    sw.WriteLine(HostName() + "\t"+IpAddress() +"\t" +System.DateTime.Now.ToString("yyyyMMdd hhmmss")+"\t" + m_sLogDetailInfo);   
   }
   sw.Close();
   sw =null;

  }  
  private static string HostName()
  {
   m_sLogOutHostName = System.Net.Dns.GetHostName();

   return m_sLogOutHostName;
  }
  private  static string IpAddress()
  {
   m_sLogOutIpAddress = System.Net.Dns.GetHostByName(m_sLogOutHostName);

   return m_sLogOutIpAddress.AddressList[0].ToString();
  }

That's all!!     ~.~

posted @ 2007-04-12 11:54  糊涂小猪  阅读(426)  评论(0编辑  收藏  举报