程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> .net日志操作類

.net日志操作類

編輯:C#入門知識

public class LogManager
{
    private static string logPath = string.Empty;
    /// <summary>
    /// 保存日志的文件夾
    /// </summary>
    public static string LogPath
    {
        get
        {
            if (logPath == string.Empty)
            {
                if (System.Web.HttpContext.Current == null)
                    // Windows Forms 應用
                    logPath = AppDomain.CurrentDomain.BaseDirectory;
                else
                    // Web 應用
                    logPath = AppDomain.CurrentDomain.BaseDirectory + @"bin";
            }
            return logPath;
        }
        set{ logPath = value;}
    }

    private static string logFielPrefix = string.Empty;
    /// <summary>
    /// 日志文件前綴
    /// </summary>
    public static string LogFielPrefix
    {
        get { return logFielPrefix; }
        set { logFielPrefix = value; }
    }

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

    /// <summary>
    /// 寫日志
    /// </summary>
    public static void WriteLog(LogFile logFile, string msg)
    {
        WriteLog(logFile.ToString(), msg);
    }
}

/// <summary>
/// 日志類型
/// </summary>
public enum LogFile
{
    Trace,
    Warning,
    Error,
    SQL
}
 

    

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved