93 lines
3.4 KiB
C#
93 lines
3.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Tool
|
|
{
|
|
public class LogHelper
|
|
{
|
|
private static readonly object LogLock = new object();
|
|
public static void WriteLog(string msg)
|
|
{
|
|
lock (LogLock)
|
|
{
|
|
string filePath = AppDomain.CurrentDomain.BaseDirectory + "Log";
|
|
if (!Directory.Exists(filePath))
|
|
{
|
|
Directory.CreateDirectory(filePath);
|
|
}
|
|
string logPath = AppDomain.CurrentDomain.BaseDirectory + "Log\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
|
|
try
|
|
{
|
|
using (StreamWriter sw = File.AppendText(logPath))
|
|
{
|
|
sw.WriteLine("**************************************************");
|
|
sw.WriteLine("时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
|
|
sw.WriteLine(msg);
|
|
sw.WriteLine();
|
|
sw.Flush();
|
|
sw.Close();
|
|
sw.Dispose();
|
|
}
|
|
}
|
|
catch (IOException e)
|
|
{
|
|
using (StreamWriter sw = File.AppendText(logPath))
|
|
{
|
|
|
|
sw.WriteLine("**************************************************");
|
|
sw.WriteLine("时间:" + DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"));
|
|
sw.WriteLine("异常:" + e.Message);
|
|
sw.WriteLine();
|
|
sw.Flush();
|
|
sw.Close();
|
|
sw.Dispose();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public static void WriteLogMater(string msg)
|
|
{
|
|
lock (LogLock)
|
|
{
|
|
string filePath = AppDomain.CurrentDomain.BaseDirectory + "Logmater";
|
|
if (!Directory.Exists(filePath))
|
|
{
|
|
Directory.CreateDirectory(filePath);
|
|
}
|
|
string logPath = AppDomain.CurrentDomain.BaseDirectory + "Logmater\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
|
|
try
|
|
{
|
|
using (StreamWriter sw = File.AppendText(logPath))
|
|
{
|
|
sw.WriteLine("**************************************************");
|
|
sw.WriteLine("时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
|
|
sw.WriteLine(msg);
|
|
sw.WriteLine();
|
|
sw.Flush();
|
|
sw.Close();
|
|
sw.Dispose();
|
|
}
|
|
}
|
|
catch (IOException e)
|
|
{
|
|
using (StreamWriter sw = File.AppendText(logPath))
|
|
{
|
|
|
|
sw.WriteLine("**************************************************");
|
|
sw.WriteLine("时间:" + DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"));
|
|
sw.WriteLine("异常:" + e.Message);
|
|
sw.WriteLine();
|
|
sw.Flush();
|
|
sw.Close();
|
|
sw.Dispose();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|