Loading...

          java .net

          C# 寫日志到文件

          using System;
          using System.Collections.Generic;
          using System.Text;
          using System.Windows.Forms;
          using System.IO;

          namespace psms.util
          {
              class Log
              {
                  /// <summary>
                  /// 寫日志文件
                  /// </summary>
                  /// <param name="sMsg"></param>
                  public static void WriteLog(string sMsg)
                  {
                      if (sMsg != "")
                      {
                          //Random randObj = new Random(DateTime.Now.Millisecond);
                          //int file = randObj.Next() + 1;
                          string filename = DateTime.Now.ToString("yyyyMM") + ".log";
                          try
                          {
                              FileInfo fi = new FileInfo(Application.StartupPath + "\\log\\" + filename);
                              if (!fi.Exists)
                              {
                                  using (StreamWriter sw = fi.CreateText())
                                  {
                                      sw.WriteLine(DateTime.Now + "\n" + sMsg + "\n");
                                      sw.Close();
                                  }
                              }
                              else
                              {
                                  using (StreamWriter sw = fi.AppendText())
                                  {
                                      sw.WriteLine(DateTime.Now + "\n" + sMsg + "\n");
                                      sw.Close();
                                  }
                              }
                          }
                          catch(Exception ex)
                          {
                              Console.WriteLine(ex.Message);
                          }
                      }
                  }
              }
          }

          posted on 2008-08-26 22:06 閱讀(1737) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          公告

          希望有一天

          我能用鼠標雙擊我的錢包

          然后選中一張100元

          按住“ctrl+c”

          接著不停的“ctrl+v”

          嘻嘻~~~笑醒~~~



          導航

          <2008年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          統計

          常用鏈接

          留言簿(6)

          隨筆分類(102)

          隨筆檔案(398)

          文章分類

          文章檔案(10)

          有趣網絡

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 措勤县| 山东| 汤原县| 宁远县| 慈溪市| 许昌县| 都兰县| 任丘市| 宁晋县| 肇东市| 凤城市| 哈密市| 班玛县| 大埔县| 神农架林区| 广西| 阆中市| 汪清县| 昭苏县| 紫金县| 涞源县| 濮阳市| 巨鹿县| 赣州市| 彭山县| 泊头市| 牡丹江市| 扶风县| 阿合奇县| 峡江县| 长沙市| 永福县| 云南省| 开远市| 广河县| 峨眉山市| 天峻县| 筠连县| 昭平县| 万安县| 萝北县|