程式log日誌 c#

來源:互聯網
上載者:User

 

日誌,對程式的排查錯誤很有用處,現寫了一個日誌的類。共用之:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace NetLog
  5. {
  6.     public static class NetLog
  7.     {
  8.         static string _logFileName = DateTime.Now.Day.ToString() + " Log";
  9.         static string _fileDirectory = System.Environment.CurrentDirectory + "//PQDlog" + "//" + DateTime.Now.Year.ToString()
  10.                     + "-" + DateTime.Now.Month.ToString();
  11.         private static void checkPath()
  12.         {
  13.             if (!System.IO.Directory.Exists(_fileDirectory))
  14.             {
  15.                 System.IO.Directory.CreateDirectory(_fileDirectory);
  16.             }
  17.         }
  18.         private static void checkFile(string fileName)
  19.         {
  20.             System.IO.StreamWriter sw;
  21.             fileName = _fileDirectory + "//" + fileName+".log";
  22.             if (!System.IO.File.Exists(fileName))
  23.             {
  24.                 //如果不存在此檔案,那麼建立它。
  25.                sw= System.IO.File.CreateText(fileName);
  26.                sw.Close();
  27.             }
  28.         }
  29.         public static void WriteLog(string loginfo)
  30.         {
  31.             checkPath();
  32.             checkFile(_logFileName);
  33.             string fileName = _fileDirectory + "//" + _logFileName+".log";
  34.             System.IO.FileStream filestream = new System.IO.FileStream(fileName, System.IO.FileMode.Open| System.IO.FileMode.Create,
  35.                     System.IO.FileAccess.Write,System.IO.FileShare.None);
  36.             System.IO.StreamWriter writer = new System.IO.StreamWriter(filestream,System.Text.Encoding.Default);
  37.             
  38.             writer.BaseStream.Seek(0, System.IO.SeekOrigin.End);
  39.             
  40.             writer.WriteLine("{0}--{1}", DateTime.Now.TimeOfDay, loginfo);
  41.             writer.Flush();
  42.             writer.Close();
  43.             filestream.Close();
  44.         }
  45.         /// <summary>
  46.         /// this log's filename.
  47.         /// </summary>
  48.         public static string LogFileName
  49.         {
  50.             get { return _logFileName; }
  51.             set { _logFileName = value; }
  52.         }
  53.         /// <summary>
  54.         /// this log's directory.
  55.         /// </summary>
  56.         public static string FileDirectory
  57.         {
  58.             get { return _fileDirectory; }
  59.             set { _fileDirectory = value; }
  60.         }
  61.     }
  62. }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.