zoukankan      html  css  js  c++  java
  • 常用的工具类3-日志类

    public class LogHelper
    {
    private static string _logFielPrefix = string.Empty;

    private static string LogPath
    {
    get
    {

    return GetLogPath(@"/log/");

    }
    }

    /// <summary>
    /// 日志文件前缀
    /// </summary>
    public static string LogFielPrefix
    {
    get { return _logFielPrefix; }
    set { _logFielPrefix = value; }
    }

    public static void WriteLog(string title, Exception ex)
    {
    WriteLog(title);
    WriteLog(ex);
    }

    /// <summary>
    /// 写日志
    /// </summary>
    public static void WriteLog(Exception ex)
    {
    WriteLog(ex.Message);
    WriteLog(ex.StackTrace);
    WriteLog(ex.Source);
    }

    public static void WriteLog(string title, SqlException ex, IDbCommand command)
    {
    WriteLog(title);
    WriteLog(ex, command);
    }

    /// <summary>
    /// 写日志 - DbCommand 会记录sql的详细信息
    /// </summary>
    public static void WriteLog(SqlException ex, IDbCommand command)
    {
    LogHelper.LogFielPrefix ="SQLErr";
    StringBuilder sb = new StringBuilder("----------- begin----------- 信息: ");
    //异常信息
    sb.AppendFormat("{0} n {1} {2} Sql信息: ", ex.Message, ex.StackTrace, ex.Source);
    sb.AppendFormat("{0} {1} ", command.CommandType.ToString(), command.CommandText);
    sb.Append("参数 : ");
    if (command.Parameters != null)
    {
    foreach (SqlParameter param in command.Parameters)
    {
    sb.Append(string.Format("{0}:{1}", param.ParameterName, param.Value));
    sb.Append(" ");
    }
    }
    //SQL详细信息
    WriteLog(sb.ToString());
    }

    /// <summary>
    /// 写日志
    /// </summary>
    public static void WriteLog(string logContent)
    {
    try
    {
    if (!Directory.Exists(LogPath))
    Directory.CreateDirectory(LogPath);

    System.IO.StreamWriter sw = System.IO.File.AppendText(
    LogPath + LogFielPrefix + " " +
    DateTime.Now.ToString("yyyyMMdd") + "log.Log"
    );
    sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss: ") + logContent);
    sw.Close();
    }
    catch (Exception ex)
    {
    }
    }
    /// <summary>
    /// 向子文件夹下写日志文件
    /// </summary>
    /// <param name="dir">子文件夹</param>
    /// <param name="logContent">文件内容</param>
    public static void WriteLog(string dir, string logContent)
    {
    try
    {
    string logPath = LogHelper.LogPath;
    if (dir != "")
    {
    if (dir.StartsWith("/") || dir.StartsWith("\"))
    dir = dir.Substring(1);
    if (!dir.EndsWith("/") && !dir.EndsWith("\"))
    {
    dir = dir + "\";
    }
    logPath = logPath + dir;
    }
    if (!Directory.Exists(logPath))
    Directory.CreateDirectory(logPath);

    System.IO.StreamWriter sw = System.IO.File.AppendText(
    logPath + LogFielPrefix + " " +
    DateTime.Now.ToString("yyyyMMdd") + "log.Log"
    );
    sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss: ") + logContent);
    sw.Close();
    }
    catch (Exception ex)
    {
    }
    }
    /// <summary>
    /// 获得当前绝对路径含web和winform应用程序方法
    /// </summary>
    /// <param name="strPath">指定的路径</param>
    /// <returns>绝对路径</returns>
    private static string GetLogPath(string strPath)
    {
    if (strPath.StartsWith("~/"))
    strPath = strPath.Substring(2);
    else if (strPath.StartsWith("/"))
    strPath = strPath.Substring(1);
    if (System.Web.HttpContext.Current != null)
    {
    //如果是web
    return System.IO.Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~"), strPath);
    }
    else //非web程序引用
    {
    return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
    }
    }
    }
    /// <summary>
    /// 日志类型
    /// </summary>
    public enum LogFile
    {
    Error,
    Info
    }

  • 相关阅读:
    OC-内存管理-基本原理与引用计数器
    OC-改错题
    OC-Q&A
    OC-SEL
    CO-类的本质、description方法
    Tomcat 下 mysql的连接池配置和使用
    转:JAVA.NET.SOCKETEXCEPTION: TOO MANY OPEN FILES解决方法
    使应用程序常驻内存,不能被任务管理器关闭之配置文件设置
    解决Tomcat catalina.out 不断成长导致档案过大的问题
    >/dev/null 2>&1的含义
  • 原文地址:https://www.cnblogs.com/zhshlimi/p/5605533.html
Copyright © 2011-2022 走看看