C# 写日志的类文件

 using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

/// <summary>
/// 记录日志
/// </summary>
public class Log
{
    public Log()
    {

    }

    /// <summary>
    /// 日志信息
    /// </summary>
    /// <param name="message"></param>
    public static void WriteLog(string message)
    {
        try
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + @"Log\";
            path += DateTime.Today.ToString("yyyyMM") + @"\";

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            string strFileName = DateTime.Today.ToString("yyyyMMdd") + ".txt";
            using (StreamWriter output = new StreamWriter(path.Trim() + strFileName.Trim(), true, System.Text.Encoding.UTF8))
            {
                output.WriteLine("============================================");
                output.WriteLine("Time: " + System.DateTime.Now.ToString());
                output.WriteLine(message.Trim());
                output.WriteLine("============================================");
                output.Flush();
                output.Close();
            }
        }
        catch (Exception e)
        {
            System.Diagnostics.Trace.WriteLine(e.Message);
        }
    }
}

你可能感兴趣的:(C# 写日志的类文件)