标签:
/// <summary>
/// 全局日志文件记录
/// </summary>
public class Loger
{
private static Object o = new Object();
public static string logBasePath = "";
/// <summary>
/// 写日志
/// </summary>
/// <param name="msg">要写的内容</param>
/// <returns></returns>
public static void Writer(string msg)
{
#region lock
lock (o)
{
try
{
try
{
if (!Directory.Exists(logBasePath))
{
Directory.CreateDirectory(logBasePath);
}
}
catch (Exception)
{
logBasePath="C:/Error";
if (!Directory.Exists(logBasePath))
{
Directory.CreateDirectory(logBasePath);
}
}
string fileFullPath = logBasePath + @"\" + "log_" + DateTime.Now.ToString("yyyy-MM-dd") + "Server.txt";
if (!File.Exists(fileFullPath))
{
FileStream fs = File.Create(fileFullPath);
fs.Close();
}
try
{
FileStream fs = File.Open(fileFullPath, FileMode.Append, FileAccess.Write, FileShare.Write);
StreamWriter sw = new StreamWriter(fs);
string time = DateTime.Now.ToString("hh:mm:ss:ffffff");
sw.Write("=========== " + time + " =================" + System.Environment.NewLine);
sw.Write(msg + System.Environment.NewLine);
sw.Write("=========== " + time + " ===================" + System.Environment.NewLine + System.Environment.NewLine);
sw.Flush();
sw.Close();
fs.Close();
}
catch (Exception)
{
}
}
catch (Exception)
{
}
}
#endregion
}
/// <summary>
/// 构造参数列表
/// </summary>
/// <param name="parNames"></param>
/// <param name="args"></param>
/// <returns></returns>
public static string ConnectObjectsString(string[] parNames, object[] args)
{
try
{
string strOut = System.Environment.NewLine;
for (int i = 0; i < parNames.Length; i++)
{
object o = args[i];
string name = parNames[i];
if (o == null)
{
strOut += name + " :" + " null " + System.Environment.NewLine;
}
else
{
strOut += name + " :" + o.ToString() + System.Environment.NewLine;
}
}
return strOut;
}
catch (Exception)
{
}
return "";
}
public static void Writer(Exception ex, string strMsg, string[] parNames, object[] args)
{
try
{
if (strMsg == null)
{
strMsg = "";
}
string str = Loger.ConnectObjectsString(new string[] { "ex.Message", "ex.StackTrace" }, new object[] { ex.Message, ex.StackTrace });
str += Loger.ConnectObjectsString(parNames, args);
Loger.Writer(strMsg + str);
}
catch (Exception)
{
}
}
public static void Writer(Exception ex, string strMsg)
{
try
{
if (strMsg == null)
{
strMsg = "";
}
string str = Loger.ConnectObjectsString(new string[] { "ex.Message", "ex.StackTrace" }, new object[] { ex.Message, ex.StackTrace });
Loger.Writer(strMsg + str);
}
catch (Exception)
{
}
}
//private Common.Log.LogWriter<SqlHelperCom> _logger = Common.Log.LogWriter<SqlHelperCom>.GetLogger();
//public Common.Log.LogWriter<SqlHelperCom> Logger
//{
// get { return _logger; }
// set { _logger = value; }
//}
}
public class LogPrompt
{
public static void Write(string ErrorStr)
{
Debug.WriteLine(ErrorStr);
}
}
public class MyLoger
{
private static Object o = new Object();
public static string logBasePath = "";
/// <summary>
/// 构造参数列表
/// </summary>
/// <param name="parNames"></param>
/// <param name="args"></param>
/// <returns></returns>
private static string ConnectObjectsString(string[] parNames, object[] args)
{
try
{
string strOut = System.Environment.NewLine;
for (int i = 0; i < parNames.Length; i++)
{
object o = args[i];
string name = parNames[i];
if (o == null)
{
strOut += name + " :" + " null " + System.Environment.NewLine;
}
else
{
strOut += name + " :" + o.ToString() + System.Environment.NewLine;
}
}
return strOut;
}
catch (Exception)
{
}
return "";
}
//消息是否为空
private static string ismsgnull(string strMsg)
{
if (strMsg == null)
{
strMsg = "";
}
return strMsg;
}
//构建日志存放路径
private static string getlogfilepath()
{
if (!Directory.Exists(logBasePath))
{
Directory.CreateDirectory(logBasePath);
}
string fileFullPath = logBasePath + @"\" + "log_" + DateTime.Now.ToString("yyyy-MM-dd") + "Server.txt";
if (!File.Exists(fileFullPath))
{
FileStream fs = File.Create(fileFullPath);
fs.Close();
}
return fileFullPath;
}
#region 写日志
/// <summary>
/// 写日志
/// </summary>
/// <param name="msg">要写的内容</param>
/// <returns></returns>
public static void Writer(string msg)
{
#region lock
lock (o)
{
try
{
string fileFullPath = getlogfilepath();
try
{
FileStream fs = File.Open(fileFullPath, FileMode.Append, FileAccess.Write, FileShare.Write);
StreamWriter sw = new StreamWriter(fs);
string time = DateTime.Now.ToString("hh:mm:ss:ffffff");
sw.Write("=========== " + time + " =================" + System.Environment.NewLine);
sw.Write(msg + System.Environment.NewLine);
sw.Write("=========== " + time + " ===================" + System.Environment.NewLine + System.Environment.NewLine);
sw.Flush();
sw.Close();
fs.Close();
}
catch (Exception)
{
}
}
catch (Exception)
{
}
}
#endregion
}
public static void Writer(Exception ex, string strMsg, string[] parNames, object[] args)
{
try
{
strMsg = ismsgnull(strMsg);
string str = MyLoger.ConnectObjectsString(new string[] { "ex.Message", "ex.StackTrace" }, new object[] { ex.Message, ex.StackTrace });
str += MyLoger.ConnectObjectsString(parNames, args);
MyLoger.Writer(strMsg + str);
}
catch (Exception)
{
}
}
public static void Writer(Exception ex, string strMsg)
{
try
{
strMsg = ismsgnull(strMsg);
string str = MyLoger.ConnectObjectsString(new string[] { "ex.Message", "ex.StackTrace" }, new object[] { ex.Message, ex.StackTrace });
MyLoger.Writer(strMsg + str);
}
catch (Exception)
{
}
}
#endregion
}
标签:
原文地址:http://www.cnblogs.com/longkai178/p/5815022.html