C#寫日志文件幫助類


1、日志幫助類:

using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Text;

/// <summary>
/// Summary description for NetLog
/// </summary>
public class NetLog
{
    /// <summary>
    /// 寫入日志到文本文件
    /// </summary>
    /// <param name="strMessage">日志內容</param>
    public static void WriteTextLog(string strMessage)
    {
        string path = AppDomain.CurrentDomain.BaseDirectory + @"Log\";
        if (!Directory.Exists(path))
            Directory.CreateDirectory(path);
        DateTime time = DateTime.Now;
        string fileFullPath = path + time.ToString("yyyy-MM-dd") + ".System.txt";
        StringBuilder str = new StringBuilder();
       
        str.Append("Time:"+ time + ";Message: " + strMessage + "\r\n");
        StreamWriter sw;
        if (!File.Exists(fileFullPath))
        {
            sw = File.CreateText(fileFullPath);
        }
        else
        {
            sw = File.AppendText(fileFullPath);
        }
        sw.WriteLine(str.ToString());
        sw.Close();
    }
}

2、調用方法:

NetLog.WriteTextLog("異常錯誤信息:"+ex.Message);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM