zoukankan      html  css  js  c++  java
  • C# 基础

    public static class LogHelper
    {
        private static readonly string _baseDir = AppDomain.CurrentDomain.BaseDirectory +  "VILog";
        private static DateTime _currentDate;
        private static StreamWriter _sw;
    
        static LogHelper()
        {
            DirectoryInfo di = new DirectoryInfo(_baseDir);
            if (!di.Exists)
                di.Create();
        }
    
        private static void CheckLogFile()
        {
            if (_sw == null)
            {
                _currentDate = DateTime.Now.Date;
                _sw = new StreamWriter(string.Format(_baseDir + "/{0}.{1}-{2}-{3}.log", Process.GetCurrentProcess().ProcessName, _currentDate.Year, _currentDate.Month, _currentDate.Day), true, Encoding.UTF8);
                return;
            }
    
            DateTime dt = DateTime.Now.Date;
            if (DateTime.Compare(dt, _currentDate) != 0)
            {
                _sw.Close();
                _sw = null;
                _currentDate = dt;
                CheckLogFile();
            }
        }
    
        public static void Log(string str, object sender = null, LogLevel level = LogLevel.Info, string detail = null)
        {
            if (IsStopLog) return;
    
            CheckLogFile();
    
            if (string.IsNullOrEmpty(str)) return;
    
            _sw.WriteLine("[{0}.{1}]:[{2}]{3}", DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString(), level, str);
    
            if (!string.IsNullOrEmpty(detail))
                _sw.WriteLine("Detail:{0}", detail);
    
            _sw.Flush();
        }
    
        public static void Info(string str, object sender = null, string detail = null)
        {
            Log(str, sender, LogLevel.Info, detail);
        }
    
        public static void Error(string str, object sender = null, string detail = null)
        {
            Log(str, sender, LogLevel.Error, detail);
        }
    
        public static void Error(string str, object sender = null, Exception ex = null)
        {
            string errmsg = ex.StackTrace?.ToString();
    
            Log(str, sender, LogLevel.Error, Environment.NewLine + errmsg);
        }
    
        public static void Warning(string str, object sender = null, string detail = null)
        {
            Log(str, sender, LogLevel.Warning, detail);
        }
    
        public static bool IsStopLog { get; set; }
    }
    
    public enum LogLevel
    {
        Info,
        Error,
        Warning
    }
    
  • 相关阅读:
    MySQL的FORMAT函数用法规则
    jetbrains idea/webstorm等(注册,激活,破解码,一起支持正版,最新可用)(2017.3.16更新)【转】
    用户价值模型 CITE :https://www.jianshu.com/p/34199b13ffbc
    用户生命周期模型
    机器学习十大常用算法(CITE 不会停的蜗牛 ) interesting
    Linux 安装Oracle11g完整安装图文教程另附基本操作 (分享)
    oracle 命中率
    SQL学习总结笔记
    hash join
    Tomcat详细安装配置
  • 原文地址:https://www.cnblogs.com/MichaelLoveSna/p/14475292.html
Copyright © 2011-2022 走看看