You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
5.1 KiB
136 lines
5.1 KiB
|
|
using DBUtility;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace PunchAndWeld
|
|
{
|
|
public class LogHelper
|
|
{
|
|
private static string CodeVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString().Trim();
|
|
|
|
//<summary>
|
|
//保存日志的文件夹
|
|
//<summary>
|
|
private static string logPath = AppDomain.CurrentDomain.BaseDirectory + @"log\";
|
|
|
|
/// <summary>
|
|
/// 写日志
|
|
/// </summary>
|
|
/// <param name="msg"></param>
|
|
/// <param name="errorFile"></param>
|
|
public static void WriteLog(string msg, string errorFile = "")
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(msg))
|
|
{
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
msg = string.Format("程序版本号:{0},Time:{1},Message:{2}", CodeVersion, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff"), msg);
|
|
}
|
|
//如果不存在log文件夹 则创建
|
|
if (!Directory.Exists(logPath))
|
|
{
|
|
Directory.CreateDirectory(logPath);
|
|
}
|
|
|
|
StreamWriter sw = File.AppendText(logPath + errorFile + DateTime.Now.ToString("yyyyMMdd") + ".Log");
|
|
sw.WriteLine(msg);
|
|
sw.Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 拼接错误日志
|
|
/// </summary>
|
|
/// <param name="ex">异常类</param>
|
|
/// <param name="errorFile">异常类文件夹名,可为空</param>
|
|
public static void WriteLogManager(Exception ex, string errorFile = "")
|
|
{
|
|
StringBuilder str = new StringBuilder();//保存到文件中的日志信息
|
|
str.AppendLine("****************************异常文本****************************");
|
|
str.AppendLine("【程序版本号】:" + CodeVersion + " 【出现时间】:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
|
|
if (ex != null)
|
|
{
|
|
str.AppendLine(string.Format("【异常类型】:{0}\r\n【异常信息】:{1}\r\n【异常方法】:{2}\r\n【堆栈调用】:{3}", ex.GetType().Name, ex.Message, ex.TargetSite, ex.StackTrace));
|
|
}
|
|
else
|
|
{
|
|
str.AppendLine(string.Format("【未处理应用程序线程错误】:{0}", ex));
|
|
}
|
|
str.AppendLine("****************************************************************");
|
|
//保存日志
|
|
WriteLog(str.ToString(), "Error" + errorFile);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 系统日志(写数据库)
|
|
/// </summary>
|
|
/// <param name="msg"></param>
|
|
/// <param name="method"></param>
|
|
public static void WriteSysLogBase(string msg, string method)
|
|
{
|
|
try
|
|
{
|
|
if (msg.Contains("'"))
|
|
{
|
|
msg = msg.Replace("'", "''");
|
|
}
|
|
string sql = @" INSERT INTO [dbo].[LogSys]
|
|
([ID]
|
|
,[SysContent]
|
|
,[SysSource]
|
|
,[CreateTime])
|
|
VALUES
|
|
((SELECT NEWID())
|
|
,'" + msg + @"'
|
|
,'" + method + @"'
|
|
,(SELECT GETDATE())) ";
|
|
SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnString, CommandType.Text, sql, null);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 错误日志(写数据库)
|
|
/// </summary>
|
|
/// <param name="msg"></param>
|
|
/// <param name="method"></param>
|
|
public static void WriteErrLogBase(string msg, string method)
|
|
{
|
|
try
|
|
{
|
|
string sql = @"
|
|
INSERT INTO [dbo].[LogErr]
|
|
([ID]
|
|
,[ErrContent]
|
|
,[ErrSource]
|
|
,[ErrTime])
|
|
VALUES
|
|
((SELECT NEWID())
|
|
,'" + msg + @"'
|
|
,'" + method + @"'
|
|
,(SELECT GETDATE()))
|
|
";
|
|
SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnString, CommandType.Text, sql, null);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|