using System;
|
using System.Collections;
|
using System.Diagnostics;
|
using System.IO;
|
using System.Reflection;
|
using System.Text;
|
using System.Threading;
|
|
namespace ZD.Cloud.Logger
|
{
|
public class LogHelper
|
{
|
private readonly static object locker = new object();
|
public static void Info(string msg, bool isAsync = true)
|
{
|
Func(LogType.Info, msg);
|
}
|
|
public static void Error(string msg, bool isAsync = true)
|
{
|
Func(LogType.Error, msg);
|
}
|
|
public static void Error(Exception ex, bool isAsync = true)
|
{
|
Func(LogType.Error, "", ex);
|
}
|
|
public static void Warn(string msg, bool isAsync = true)
|
{
|
Func(LogType.Warn, msg);
|
}
|
public static void Warn(Exception ex, bool isAsync = true)
|
{
|
Func(LogType.Warn, "", ex);
|
}
|
|
//带文件夹名称
|
public static void Error(string logFol, string msg, bool isAsync = true)
|
{
|
FuncWithFol(LogType.Error, logFol, msg);
|
}
|
public static void Error(string logFol, Exception ex, bool isAsync = true)
|
{
|
FuncWithFol(LogType.Error, logFol, "", ex);
|
}
|
public static void Warn(string logFol, string msg, bool isAsync = true)
|
{
|
FuncWithFol(LogType.Warn, logFol, msg);
|
}
|
|
public static void Warn(string logFol, Exception ex, bool isAsync = true)
|
{
|
FuncWithFol(LogType.Warn, logFol, "", ex);
|
}
|
|
public static void Info(string logFol, string msg, bool isAsync = true)
|
{
|
FuncWithFol(LogType.Info, logFol, msg);
|
}
|
|
private static void Func(LogType type, string msg = "", Exception ex = null, bool isAsync = true)
|
{
|
try
|
{
|
StringBuilder sb = new StringBuilder();
|
string dt = DateTime.Now.ToString(@"yyyy-MM-dd HH:mm:ss fff");
|
sb.AppendFormat("{0} {1} - {2}", dt, type, msg);
|
if (type == LogType.Error)
|
{
|
sb.AppendFormat(" [Exception:{0}]", ex);
|
}
|
WriteLog("", sb.ToString());
|
|
if (type == LogType.Warn)
|
{
|
Mail.SendMail(sb.ToString());
|
}
|
}
|
finally
|
{
|
}
|
}
|
|
private static void FuncWithFol(LogType type, string logFol, string msg = "", Exception ex = null, bool isAsync = true)
|
{
|
try
|
{
|
StringBuilder sb = new StringBuilder();
|
string dt = DateTime.Now.ToString(@"yyyy-MM-dd HH:mm:ss fff");
|
sb.AppendFormat("{0} {1} - {2}", dt, type, msg);
|
if (type == LogType.Error)
|
{
|
sb.AppendFormat(" [Exception:{0}]", ex);
|
}
|
WriteLog(logFol, sb.ToString());
|
|
if (type == LogType.Warn)
|
{
|
Mail.SendMail(sb.ToString());
|
}
|
}
|
finally
|
{
|
}
|
}
|
|
|
private static void WriteLog(string logFol, string logContent)
|
{
|
if (string.IsNullOrWhiteSpace(logContent)) return;
|
|
if (string.IsNullOrWhiteSpace(logFol))
|
{
|
logFol = "zdlog";
|
}
|
StackTrace trace = new StackTrace();
|
StackFrame frame = trace.GetFrame(2);//1代表上级,2代表上上级,以此类推
|
MethodBase method = frame.GetMethod();
|
var className = method.ReflectedType.Name;
|
string logDir = AppDomain.CurrentDomain.BaseDirectory;
|
var logPath = System.Configuration.ConfigurationManager.AppSettings["LogPath"];
|
if (string.IsNullOrEmpty(logPath))
|
{
|
logPath = string.Format(@"{0}\{1}\{2}", logDir, logFol, className);
|
}
|
|
logPath = logPath.Replace("_", "").Replace(" ", "").Replace("/r", "").Replace("/n", "");
|
|
if (!Directory.Exists(logPath)) Directory.CreateDirectory(logPath);
|
|
string filename = string.Format(@"{0}\{1}.txt", logPath, DateTime.Now.ToString(@"yyyyMMdd"));
|
if (File.Exists(filename))
|
{
|
FileStream fs = new FileStream(filename, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
|
StreamWriter sr = new StreamWriter(fs);
|
sr.WriteLine(logContent);
|
sr.Close();
|
fs.Close();
|
}
|
else
|
{
|
FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
|
StreamWriter sr = new StreamWriter(fs);
|
sr.WriteLine(logContent);
|
sr.Close();
|
fs.Close();
|
}
|
|
}
|
|
public enum LogType
|
{
|
Info,
|
Error,
|
Warn
|
}
|
}
|
}
|