185 lines
5.5 KiB
C#
185 lines
5.5 KiB
C#
using Epost.Model;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
|
|
namespace Epost.Common
|
|
{
|
|
public class LogHelper
|
|
{
|
|
/// <summary>
|
|
/// 输出日志到Log4Net
|
|
/// </summary>
|
|
/// <param name="t"></param>
|
|
/// <param name="ex"></param>
|
|
#region static void WriteLog(Type t, Exception ex)
|
|
|
|
public static void WriteLog(Type t, Exception ex)
|
|
{
|
|
log4net.ILog log = log4net.LogManager.GetLogger(t);
|
|
log.Error("Error", ex);
|
|
}
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// 输出日志到Log4Net
|
|
/// </summary>
|
|
/// <param name="t"></param>
|
|
/// <param name="msg"></param>
|
|
///
|
|
#region static void WriteLog(Type t, string msg)
|
|
|
|
public static void WriteLog(Type t, string msg)
|
|
{
|
|
log4net.ILog log = log4net.LogManager.GetLogger(t);
|
|
log.Error(msg);
|
|
}
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// 输出日志到Log4Net
|
|
/// </summary>
|
|
/// <param name="t"></param>
|
|
/// <param name="msg"></param>
|
|
///
|
|
#region static void WriteLog(Type t, string msg,Exception ex)
|
|
|
|
public static void WriteLog(Type t, string msg, Exception ex)
|
|
{
|
|
log4net.ILog log = log4net.LogManager.GetLogger(t);
|
|
log.Error(msg, ex);
|
|
}
|
|
|
|
#endregion
|
|
|
|
public enum Log_Type
|
|
{
|
|
[Description("错误日志")]
|
|
ERROR,
|
|
[Description("点亮设备日志")]
|
|
LIGHTUP,
|
|
[Description("任务拍下日志")]
|
|
JOB_DONE,
|
|
[Description("信息记录")]
|
|
INFO
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 输出日志到Log4Net
|
|
/// </summary>
|
|
/// <param name="t"></param>
|
|
/// <param name="ex"></param>
|
|
public static void WriteLogInfo(string msg, Log_Type type = Log_Type.INFO)
|
|
{
|
|
try
|
|
{
|
|
|
|
if (type == Log_Type.LIGHTUP)
|
|
{
|
|
log4net.ILog securityLogger = log4net.LogManager.GetLogger("LightUp");
|
|
securityLogger.Info(msg);
|
|
}
|
|
else if (type == Log_Type.JOB_DONE)
|
|
{
|
|
log4net.ILog securityLogger = log4net.LogManager.GetLogger("JobDown");
|
|
securityLogger.Info(msg);
|
|
}
|
|
else if (type == Log_Type.INFO)
|
|
{
|
|
log4net.ILog securityLogger = log4net.LogManager.GetLogger("System_Log");
|
|
securityLogger.Info(msg);
|
|
}
|
|
else if (type == Log_Type.ERROR)
|
|
{
|
|
log4net.ILog securityLogger = log4net.LogManager.GetLogger("ErrorLog");
|
|
securityLogger.Info(msg);
|
|
}
|
|
else { }
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
WtiteLog("写日志异常:" + ex.Message);
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 输出日志到Log4Net
|
|
/// </summary>
|
|
/// <param name="t"></param>
|
|
/// <param name="ex"></param>
|
|
public static void WriteLogEx(Exception msg, Log_Type type = Log_Type.INFO)
|
|
{
|
|
try
|
|
{
|
|
|
|
if (type == Log_Type.LIGHTUP)
|
|
{
|
|
log4net.ILog securityLogger = log4net.LogManager.GetLogger("LightUp");
|
|
securityLogger.Info(msg);
|
|
}
|
|
else if (type == Log_Type.JOB_DONE)
|
|
{
|
|
log4net.ILog securityLogger = log4net.LogManager.GetLogger("JobDown");
|
|
securityLogger.Info(msg);
|
|
}
|
|
else if (type == Log_Type.INFO)
|
|
{
|
|
log4net.ILog securityLogger = log4net.LogManager.GetLogger("System_Log");
|
|
securityLogger.Info(msg);
|
|
}
|
|
else if (type == Log_Type.ERROR)
|
|
{
|
|
log4net.ILog securityLogger = log4net.LogManager.GetLogger("ErrorLog");
|
|
securityLogger.Info(msg);
|
|
}
|
|
else { }
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
WtiteLog("写日志异常:" + ex.Message);
|
|
}
|
|
}
|
|
#region 自定义输出日志文件
|
|
public static void WtiteLog(string ex)
|
|
{
|
|
//如果是同一天的话,则打开文件在末尾写入。如果不是同一天,则创建文件写入文件
|
|
|
|
//判断是否存在文件
|
|
if (File.Exists(DateTime.Today.ToString("yyyyMMdd") + ".log"))
|
|
{
|
|
//如果存在文件,则向文件添加日志
|
|
StreamWriter sw = new StreamWriter(DateTime.Today.ToString("yyyyMMdd") + ".log", true);
|
|
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":");
|
|
sw.WriteLine(ex);
|
|
sw.Close();
|
|
return;
|
|
|
|
}
|
|
|
|
//如果文件不存在,则创建文件后向文件添加日志
|
|
StreamWriter sw2 = new StreamWriter(DateTime.Today.ToString("yyyyMMdd") + ".log", true);
|
|
sw2.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":");
|
|
sw2.WriteLine(ex);
|
|
sw2.Close();
|
|
|
|
|
|
}
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|