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