常用的工具类3-日志类

Posted zslm___

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了常用的工具类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
}

以上是关于常用的工具类3-日志类的主要内容,如果未能解决你的问题,请参考以下文章

Android快速开发系列 10个常用工具类

Flutter Utils 全网最齐全的工具类

StringUtils工具类常用方法汇总

java 啥叫工具类

FlutterUtils 工具库

Mybatis常用工具类-SQL类