2 Star 33 Fork 23

Sunday / 代码片段

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
LogHelper.cs 1.79 KB
一键复制 编辑 原始数据 按行查看 历史
Sunday 提交于 2021-08-13 05:28 . 上传简单的日志工具类
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace WinMonitor
{
//https://www.cnblogs.com/aqing0/p/11460550.html 参考。适用于低版本的framework 简单的环境
public class LogHelper
{
//在网站根目录下创建日志目录(bin文件夹→debug文件夹→logs文件夹)
public static string path = AppDomain.CurrentDomain.BaseDirectory + "logs";
//死锁
public static object loglock = new object();
public static void Debug(string content)
{
WriteLog("DEBUG", content);
}
public static void Info(string content)
{
WriteLog("INFO", content);
}
public static void Error(string content)
{
WriteLog("ERROR", content);
}
protected static void WriteLog(string type, string content)
{
lock (loglock)
{
if (!Directory.Exists(path))//如果日志目录不存在就创建
{
Directory.CreateDirectory(path);
}
string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff");//获取当前系统时间
string filename = path + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";//用日期对日志文件命名
//创建或打开日志文件,向日志文件末尾追加记录
StreamWriter mySw = File.AppendText(filename);
//向日志文件写入内容
string write_content = time + " " + type + ": " + content;
mySw.WriteLine(write_content);
//关闭日志文件
mySw.Close();
}
}
}
}
C#
1
https://gitee.com/sundayisblue/code_snippet.git
git@gitee.com:sundayisblue/code_snippet.git
sundayisblue
code_snippet
代码片段
master

搜索帮助