public class Loghelper
{
private static string _logfielprefix = String. Empty;
private static string LogPath
{
Get
{
return Getlogpath (@ "/log/");
}
}
<summary>
Log file Prefix
</summary>
public static string Logfielprefix
{
get {return _logfielprefix;}
set {_logfielprefix = value;}
}
public static void Writelog (string title, Exception ex)
{
Writelog (title);
Writelog (ex);
}
<summary>
Write log
</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>
//write log-DbCommand logs details of SQL
//</summary>
public static void Writelog (SqlE Xception ex, IDbCommand command)
{
Loghelper.logfielprefix = "Sqlerr";
StringBuilder sb = new StringBuilder ("-----------begin-----------\ r \ n information:");
//exception information
SB. AppendFormat ("{0} \r\tn {1} \ r \ n {2} \r\nsql information: \ r \ n", ex. Message, ex. StackTrace, ex. Source);
SB. AppendFormat ("{0} \r\n{1} \ r \ n", command.CommandType.ToString (), command.commandtext);
SB. Append ("parameter: \ 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 details
Writelog (sb.) ToString ());
}
//<summary>
//write log
//</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>
Write a log file to a subfolder
</summary>
<param name= "dir" > sub-folders </param>
<param name= "logcontent" > File contents </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>
Get the current absolute path with Web and WinForm application methods
</summary>
<param name= "strpath" > Specified path </param>
<returns> Absolute Path </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)
{
If the web
Return System.IO.Path.Combine (System.Web.HttpContext.Current.Server.MapPath ("~"), strpath);
}
else//non-Web program reference
{
Return System.IO.Path.Combine (AppDomain.CurrentDomain.BaseDirectory, strpath);
}
}
}
<summary>
Log type
</summary>
public enum LogFile
{
Error,
Info
}
Common tool Classes 3-Log class