113 lines
3.5 KiB
C#
113 lines
3.5 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.IO;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
|
|||
|
|
namespace TestWebSocket
|
|||
|
|
{
|
|||
|
|
public class LogHelper
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 日志帮助类
|
|||
|
|
/// </summary>
|
|||
|
|
|
|||
|
|
static string LogFile = "";
|
|||
|
|
/// <summary>
|
|||
|
|
/// 在Logs文件夹(不存在则自动创建)下创建一个日志文件
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="FileName"></param>
|
|||
|
|
public static void Init()
|
|||
|
|
{
|
|||
|
|
string directory = AppDomain.CurrentDomain.BaseDirectory + "\\Logs";
|
|||
|
|
if (!Directory.Exists(directory))
|
|||
|
|
{
|
|||
|
|
Directory.CreateDirectory(directory);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
string day2 = "";
|
|||
|
|
string day3 = "";
|
|||
|
|
string day4 = "";
|
|||
|
|
string day5 = "";
|
|||
|
|
string day6 = "";
|
|||
|
|
string day7 = "";
|
|||
|
|
|
|||
|
|
DateTime curTime = DateTime.Now;
|
|||
|
|
|
|||
|
|
LogFile = directory + "\\" + curTime.ToString("yyyy-MM-dd") + ".txt";
|
|||
|
|
if (!File.Exists(LogFile))
|
|||
|
|
{
|
|||
|
|
day2 = directory + "\\" + curTime.AddDays(1).ToString("yyyy-MM-dd") + ".txt";
|
|||
|
|
day3 = directory + "\\" + curTime.AddDays(2).ToString("yyyy-MM-dd") + ".txt";
|
|||
|
|
day4 = directory + "\\" + curTime.AddDays(3).ToString("yyyy-MM-dd") + ".txt";
|
|||
|
|
day5 = directory + "\\" + curTime.AddDays(4).ToString("yyyy-MM-dd") + ".txt";
|
|||
|
|
day6 = directory + "\\" + curTime.AddDays(5).ToString("yyyy-MM-dd") + ".txt";
|
|||
|
|
day7 = directory + "\\" + curTime.AddDays(6).ToString("yyyy-MM-dd") + ".txt";
|
|||
|
|
|
|||
|
|
FileStream fs = File.Create(LogFile);
|
|||
|
|
fs.Close();
|
|||
|
|
|
|||
|
|
FileStream fs2 = File.Create(day2);
|
|||
|
|
FileStream fs3 = File.Create(day3);
|
|||
|
|
FileStream fs4 = File.Create(day4);
|
|||
|
|
FileStream fs5 = File.Create(day5);
|
|||
|
|
FileStream fs6 = File.Create(day6);
|
|||
|
|
FileStream fs7 = File.Create(day7);
|
|||
|
|
fs2.Close();
|
|||
|
|
fs3.Close();
|
|||
|
|
fs4.Close();
|
|||
|
|
fs5.Close();
|
|||
|
|
fs6.Close();
|
|||
|
|
fs7.Close();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 追加一条信息
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="text"></param>
|
|||
|
|
public static void Write(string text)
|
|||
|
|
{
|
|||
|
|
lock (LogFile)
|
|||
|
|
{
|
|||
|
|
Init();
|
|||
|
|
using (StreamWriter sw = new StreamWriter(LogFile, true, Encoding.UTF8))
|
|||
|
|
{
|
|||
|
|
sw.Write(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] ") + text);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
public static void Writeinfo(string text)
|
|||
|
|
{
|
|||
|
|
lock (LogFile)
|
|||
|
|
{
|
|||
|
|
Init();
|
|||
|
|
using (StreamWriter sw = new StreamWriter(LogFile, true, Encoding.UTF8))
|
|||
|
|
{
|
|||
|
|
sw.Write(DateTime.Now.ToString("测试数据") + text);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 追加一行信息
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="text"></param>
|
|||
|
|
public static void WriteLine(string text)
|
|||
|
|
{
|
|||
|
|
lock (LogFile)
|
|||
|
|
{
|
|||
|
|
Init();
|
|||
|
|
text += "\r\n";
|
|||
|
|
using (StreamWriter sw = new StreamWriter(LogFile, true, Encoding.UTF8))
|
|||
|
|
{
|
|||
|
|
sw.Write(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] ") + text);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|