常用的工具类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
}