一厂MES,含注塑,喷涂,冲孔
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.

74 lines
2.7 KiB

3 months ago
using System;
using System.IO;
using System.Text;
namespace AppWebservice
{
/// <summary>
/// 日志处理类
/// </summary>
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);
}
}
}