Common tool Classes 3-Log class

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.