zoukankan      html  css  js  c++  java
  • C# 简单日志帮助类LogHelper

    调用:

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

  • 相关阅读:
    前端知识体系(一)浏览器机制以及进程线程的关系
    页面之间的通信
    DOM
    浏览器内核及浏览器对象
    js正则表达式
    setTimeout和setInterval
    原生js操作cookie
    apply、call、bind方法调用
    一个简单的hexo搭建博客网站的故事
    debug for bin runtime error
  • 原文地址:https://www.cnblogs.com/aqing0/p/11460550.html
Copyright © 2011-2022 走看看