常用的工具类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-----------\r\n信息: ");
//异常信息
sb.AppendFormat("{0} \r\tn {1} \r\n {2} \r\nSql信息:\r\n", ex.Message, ex.StackTrace, ex.Source);
sb.AppendFormat("{0} \r\n{1} \r\n", command.CommandType.ToString(), command.CommandText);
sb.Append("参数 :\r\n");
if (command.Parameters != null)
{
foreach (SqlParameter param in command.Parameters)
{
sb.Append(string.Format("{0}:{1}", param.ParameterName, param.Value));
sb.Append("\r\n");
}
}
//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
}

posted @ 2016-06-22 00:36  zslm___  阅读(301)  评论(0编辑  收藏  举报