调用:

LogHelper.Debug("");

LogHelper.Info("");

LogHelper.Error("");

项目添加LogHelper类

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace AvoidMisplace
{
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();
}
}
}
}

最新文章

  1. html BOM、DOM
  2. jquery 赋值文本框输入框
  3. iOS AES加密解密实现方法
  4. 一个项目中哪些文件是要上传到 git上的,哪些是不必要的
  5. RUP(Rational Unified Process)统一软件过程概述
  6. Android Touch事件分发机制
  7. 8天学通MongoDB——第七天 运维技术
  8. UILabel + 导入字体
  9. mysql自定义循环函数
  10. gitlab一键安装+配置(备份+LADP认证)
  11. zimbra6同域名与同hostname与同系统异机恢复
  12. 应用负载均衡之LVS(五):lvs和nginx的wrr加权调度算法规律分析
  13. Python自然语言处理笔记【一】文本分类之监督式分类
  14. vue组件局部与全局注册的区别
  15. i386 x86_64 armv7 arm64
  16. MySQL:日期函数、时间函数处理(转)
  17. [LeetCode&Python] Problem 107. Binary Tree Level Order Traversal II
  18. find查找时排除目录及文件
  19. Java 面试 --- 3
  20. linu 把文件中的字母小写转换为大写,大写转换为小写awk toupper tolower

热门文章

  1. ubuntu 用户名配置及磁盘挂载
  2. fatal error C1083: 无法打开包括文件: “Halcon.h”: No such file or directory
  3. Python玩转微信小程序
  4. 201777010217-金云馨《面向对象程序设计(Java)》第十二周学习总结
  5. 如何隐藏WooCommerce Shop Page页面的标题
  6. python 并行处理数据
  7. hive数据库从文件插入数据得到结果NULL?
  8. web框架--tornado框架之初识
  9. openlayers上添加点击事件
  10. 【oracle】update select语句