日志辅助类分享

103 阅读1分钟

public class Log { private static string logPath = ""; ///

/// 保存日志的文件夹 /// public static string LogPath { get { if (logPath == string.Empty) { logPath = AppDomain.CurrentDomain.BaseDirectory; } return logPath; } set { logPath = value; } }

    /// <summary>
    /// 写日志
    /// </summary>
    public static void C_WriteLog(string msg)
    {
        try
        {
            System.IO.StreamWriter sw = System.IO.File.AppendText("C://Log123.txt");
            sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            sw.WriteLine(msg);
            sw.Close();
        }
        catch (Exception err)
        {

        }
    }

    /// <summary>
    /// 写日志
    /// </summary>
    public static void WriteLog(string msg)
    {
        bool EnabledLog = false;
        //string enableLog = ConfigurationManager.AppSettings["EnabledLog"];
        string enableLog = EnabledLog.ToString().ToLower();
        if (enableLog.Equals("true"))
            WriteLog("", msg);
    }

    /// <summary>
    /// 写日志
    /// </summary>
    public static void WriteLog(string prev, string msg)
    {
        try
        {
            System.IO.StreamWriter sw = System.IO.File.AppendText(
                LogPath + "/log/" + prev +
                DateTime.Now.ToString("yyyyMMdd") + ".Log"
                );
            sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            sw.WriteLine(msg);
            sw.Close();
        }
        catch (Exception err)
        {

        }
    }

    public static void AddLog(string logName, string LogStr)
    {
        using (StreamWriter sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "Log\\" + logName + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", true, Encoding.UTF8))
        {
            sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            sw.WriteLine(LogStr);
            sw.WriteLine("--------------------------------------------");
            sw.Close();
            sw.Dispose();
        }
    }
}