364 lines
14 KiB
C#
364 lines
14 KiB
C#
using LogServer;
|
|
using Newtonsoft.Json;
|
|
using Services.Tool;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.Entity.Validation;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
using System.Web.UI;
|
|
|
|
namespace Services.Manager
|
|
{
|
|
/// <summary>
|
|
/// 日志类
|
|
/// </summary>
|
|
public partial class Logs
|
|
{
|
|
|
|
/// <summary>
|
|
/// 锁1 路径/App_Data/Log/
|
|
/// </summary>
|
|
private static readonly object _lock = new object();
|
|
|
|
/// <summary>
|
|
/// 锁2 路径/App_Data/TimingPlan/
|
|
/// </summary>
|
|
private static readonly object _lock1 = new object();
|
|
|
|
/// <summary>
|
|
/// 锁3 路径/App_Data/UDPlog/
|
|
/// </summary>
|
|
private static readonly object _lockUdp = new object();
|
|
|
|
public static void WriteErrorLog(Exception ex)
|
|
{
|
|
try
|
|
{
|
|
|
|
if (ex != null)
|
|
{
|
|
string content = "类型:错误代码\r\n";
|
|
content += "时间:" + DateTime.Now.ToString() + "\r\n";
|
|
content += "来源:" + ex.TargetSite.ReflectedType.ToString() + "." + ex.TargetSite.Name + "\r\n";
|
|
content += "内容:" + ex.Message + "\r\n";
|
|
|
|
Page page = new Page();
|
|
HttpServerUtility server = page.Server;
|
|
string dir = server.MapPath("/App_Data/Log/");
|
|
if (!Directory.Exists(dir))
|
|
{
|
|
Directory.CreateDirectory(dir);
|
|
}
|
|
|
|
string path = dir + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
|
|
|
|
StreamWriter FileWriter = new StreamWriter(path, true, System.Text.Encoding.UTF8); //创建日志文件
|
|
FileWriter.Write("---------------------------------------------------\r\n");
|
|
FileWriter.Write(content);
|
|
FileWriter.Close(); //关闭StreamWriter对象
|
|
|
|
|
|
|
|
|
|
//}
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public static void WriteLog(string msg)
|
|
{
|
|
try
|
|
{
|
|
if (!string.IsNullOrEmpty(msg))
|
|
{
|
|
|
|
string content = "类型:调试日志\r\n";
|
|
content += "时间:" + DateTime.Now.ToString() + "\r\n";
|
|
content += "内容:" + msg + "\r\n";
|
|
|
|
Page page = new Page();
|
|
HttpServerUtility server = page.Server;
|
|
string dir = server.MapPath("/App_Data/Log/");
|
|
if (!Directory.Exists(dir))
|
|
{
|
|
Directory.CreateDirectory(dir);
|
|
}
|
|
|
|
string path = dir + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
|
|
|
|
StreamWriter FileWriter = new StreamWriter(path, true, System.Text.Encoding.UTF8); //创建日志文件
|
|
FileWriter.Write("---------------------------------------------------\r\n");
|
|
FileWriter.Write(content);
|
|
FileWriter.Close(); //关闭StreamWriter对象
|
|
//}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelp.WriteExceptionLog(ex);
|
|
}
|
|
}
|
|
|
|
public static void WriteErrorLog(string errsrc, Exception ex)
|
|
{
|
|
try
|
|
{
|
|
string errtxt = "";
|
|
string exname = ex.GetType().ToString();
|
|
if (exname == "System.Data.Entity.Validation.DbEntityValidationException")
|
|
{
|
|
foreach (var item in ((DbEntityValidationException)ex).EntityValidationErrors)
|
|
{
|
|
foreach (var err in item.ValidationErrors)
|
|
{
|
|
errtxt += "EntityValidationErrors >>>>>> “" + err.PropertyName + "”字段" + err.ErrorMessage + "\r\n";
|
|
}
|
|
}
|
|
}
|
|
string content = (!string.IsNullOrEmpty(errsrc) ? "来自页面:" + errsrc : "") + "\r\n";
|
|
content += "发生时间:" + DateTime.Now.ToString() + "\r\n";
|
|
content += "异常对像:" + ex.TargetSite.ReflectedType.ToString() + "." + ex.TargetSite.Name + "\r\n";
|
|
content += "错误追踪:" + ex.StackTrace + "\r\n";
|
|
content += "错误提示:" + ex.Message + "\r\n" + errtxt;
|
|
if (ex.InnerException != null && ex.InnerException.InnerException != null)
|
|
content += ex.InnerException.InnerException.Message + "\r\n";
|
|
//if (BaseConfigs.GetLogInDB == 1)
|
|
//{
|
|
//TSysLog log = new TSysLog();
|
|
//log.Content = content;
|
|
//log.Type = "系统日志";
|
|
//log.CreateTime = DateTime.Now;
|
|
//DataAccess.CreateSysLog().Add(log);
|
|
//}
|
|
//if (BaseConfigs.GetLogInFile == 1)
|
|
//{
|
|
Page page = new Page();
|
|
HttpServerUtility server = page.Server;
|
|
string dir = server.MapPath("/App_Data/Log/");
|
|
if (!Directory.Exists(dir))
|
|
{
|
|
Directory.CreateDirectory(dir);
|
|
}
|
|
|
|
string path = dir + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
|
|
|
|
StreamWriter FileWriter = new StreamWriter(path, true, System.Text.Encoding.UTF8); //创建日志文件
|
|
FileWriter.Write("---------------------------------------------------\r\n");
|
|
FileWriter.Write(content);
|
|
FileWriter.Close(); //关闭StreamWriter对象
|
|
//}
|
|
|
|
}
|
|
catch (Exception )
|
|
{
|
|
LogHelp.WriteExceptionLog(ex);
|
|
}
|
|
}
|
|
|
|
|
|
public static void WriteTimingPlanLog(string msg)
|
|
{
|
|
try
|
|
{
|
|
if (!string.IsNullOrEmpty(msg))
|
|
{
|
|
|
|
string content = "类型:调试日志 ";
|
|
content += "时间:" + DateTime.Now.ToString() + " ";
|
|
content += "内容:" + msg + "\r\n";
|
|
|
|
string dir = AppDomain.CurrentDomain.BaseDirectory + "/App_Data/TimingPlan/";
|
|
if (!Directory.Exists(dir))
|
|
{
|
|
Directory.CreateDirectory(dir);
|
|
}
|
|
|
|
string path = dir + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
|
|
|
|
lock (_lock1)
|
|
{
|
|
StreamWriter FileWriter = new StreamWriter(path, true, Encoding.UTF8); //创建日志文件
|
|
//FileWriter.Write("---------------------------------------------------\r\n");
|
|
FileWriter.Write(content);
|
|
FileWriter.Close(); //关闭StreamWriter对象
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelp.WriteExceptionLog(ex);
|
|
}
|
|
}
|
|
public static void WriteUserinfo(dynamic msgs)
|
|
{
|
|
try
|
|
{
|
|
string msg = JsonConvert.SerializeObject(msgs);
|
|
if (!string.IsNullOrEmpty(msg))
|
|
{
|
|
|
|
string content = "类型:调试日志 ";
|
|
content += "时间:" + DateTime.Now.ToString() + " ";
|
|
content += "内容:" + msg + "\r\n";
|
|
|
|
string dir = AppDomain.CurrentDomain.BaseDirectory + "/App_Data/Userinfo/";
|
|
if (!Directory.Exists(dir))
|
|
{
|
|
Directory.CreateDirectory(dir);
|
|
}
|
|
|
|
string path = dir + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
|
|
|
|
lock (_lock1)
|
|
{
|
|
StreamWriter FileWriter = new StreamWriter(path, true, Encoding.UTF8); //创建日志文件
|
|
//FileWriter.Write("---------------------------------------------------\r\n");
|
|
FileWriter.Write(content);
|
|
FileWriter.Close(); //关闭StreamWriter对象
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelp.WriteExceptionLog(ex);
|
|
}
|
|
}
|
|
|
|
public static void WriteErrorTimingPlanLog(string errsrc, Exception ex)
|
|
{
|
|
try
|
|
{
|
|
if (ex != null)
|
|
{
|
|
string content = (!string.IsNullOrEmpty(errsrc) ? "来自作业线程:" + errsrc : "") + "\r\n";
|
|
content += "发生时间:" + DateTime.Now.ToString() + "\r\n";
|
|
content += "异常对像:" + ex.TargetSite.ReflectedType.ToString() + "." + ex.TargetSite.Name + "\r\n";
|
|
content += "错误追踪:" + ex.StackTrace + "\r\n";
|
|
content += "错误提示:" + ex.Message + "\r\n";
|
|
if (ex.InnerException != null && ex.InnerException.InnerException != null)
|
|
content += ex.InnerException.InnerException.Message + "\r\n";
|
|
|
|
string dir = AppDomain.CurrentDomain.BaseDirectory + "/App_Data/TimingPlan/";
|
|
if (!Directory.Exists(dir))
|
|
{
|
|
Directory.CreateDirectory(dir);
|
|
}
|
|
|
|
string path = dir + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
|
|
lock (_lock1)
|
|
{
|
|
StreamWriter FileWriter = new StreamWriter(path, true, Encoding.UTF8); //创建日志文件
|
|
FileWriter.Write("---------------------------------------------------\r\n");
|
|
FileWriter.Write(content);
|
|
FileWriter.Close(); //关闭StreamWriter对象
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exl)
|
|
{
|
|
LogHelp.WriteExceptionLog(ex);
|
|
LogHelp.WriteExceptionLog(exl);
|
|
}
|
|
}
|
|
|
|
public static void WriteTimingUDPLog(string msg)
|
|
{
|
|
try
|
|
{
|
|
if (!string.IsNullOrEmpty(msg))
|
|
{
|
|
|
|
string content = "类型:调试日志 ";
|
|
content += "时间:" + DateTime.Now.ToString() + " ";
|
|
content += "内容:" + msg + "\r\n";
|
|
|
|
string dir = AppDomain.CurrentDomain.BaseDirectory + "/App_Data/UDPLog/";
|
|
if (!Directory.Exists(dir))
|
|
{
|
|
Directory.CreateDirectory(dir);
|
|
}
|
|
|
|
string path = dir + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
|
|
|
|
lock (_lockUdp)
|
|
{
|
|
StreamWriter FileWriter = new StreamWriter(path, true, Encoding.UTF8); //创建日志文件
|
|
//FileWriter.Write("---------------------------------------------------\r\n");
|
|
FileWriter.Write(content);
|
|
FileWriter.Close(); //关闭StreamWriter对象
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelp.WriteExceptionLog(ex);
|
|
}
|
|
}
|
|
|
|
public static void WriteErrorTimingUDPLog(string errsrc, Exception ex)
|
|
{
|
|
try
|
|
{
|
|
if (ex != null)
|
|
{
|
|
string content = (!string.IsNullOrEmpty(errsrc) ? "来自作业线程:" + errsrc : "") + "\r\n";
|
|
content += "发生时间:" + DateTime.Now.ToString() + "\r\n";
|
|
content += "异常对像:" + ex.TargetSite.ReflectedType.ToString() + "." + ex.TargetSite.Name + "\r\n";
|
|
content += "错误追踪:" + ex.StackTrace + "\r\n";
|
|
content += "错误提示:" + ex.Message + "\r\n";
|
|
if (ex.InnerException != null && ex.InnerException.InnerException != null)
|
|
content += ex.InnerException.InnerException.Message + "\r\n";
|
|
|
|
string dir = AppDomain.CurrentDomain.BaseDirectory + "/App_Data/UDPLog/";
|
|
if (!Directory.Exists(dir))
|
|
{
|
|
Directory.CreateDirectory(dir);
|
|
}
|
|
|
|
string path = dir + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
|
|
lock (_lockUdp)
|
|
{
|
|
StreamWriter FileWriter = new StreamWriter(path, true, Encoding.UTF8); //创建日志文件
|
|
FileWriter.Write("---------------------------------------------------\r\n");
|
|
FileWriter.Write(content);
|
|
FileWriter.Close(); //关闭StreamWriter对象
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exl)
|
|
{
|
|
LogHelp.WriteExceptionLog(exl);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static class LogHelp
|
|
{
|
|
/// <summary>
|
|
/// 写入日志
|
|
/// </summary>
|
|
/// <param name="ex">异常对象</param>
|
|
/// <param name="operationer">操作人</param>
|
|
/// <param name="actionName">功能站点名称</param>
|
|
public static void WriteExceptionLog(Exception ex, string actionName = "", string operationer = "")
|
|
{
|
|
if (HttpContext.Current == null)
|
|
{
|
|
ExceptionLogHelper.Log(new ExceptionLogMessage() { ActionName = actionName, exception = ex, Header = "", Url = "线程", Operationer = "线程", IP = "" });
|
|
}
|
|
else
|
|
{
|
|
ExceptionLogHelper.Log(new ExceptionLogMessage() { ActionName = actionName, exception = ex, Header = HttpContext.Current.Request.Headers.ToString(), Url = HttpContext.Current.Request.Url.ToString(), Operationer = operationer, IP = IPHelper.GetIP() });
|
|
}
|
|
}
|
|
}
|
|
}
|