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
}

C# 基础 - 日志捕获一使用 StreamWriter的更多相关文章

  1. C# 基础 - 日志捕获二使用 log4net

    引入 log4net.dll 项目->添加->新建项->应用程序配置文件,命名为 log4net.config,并把属性的复制到输出目录设置为 如果较新则复制,后续客户端需要读取在 ...

  2. Logan:美团点评的开源移动端基础日志库

    前言 Logan是美团点评集团移动端基础日志组件,这个名称是Log和An的组合,代表个体日志服务.同时Logan也是“金刚狼”大叔的名号,当然我们更希望这个产品能像金刚狼大叔一样犀利. Logan已经 ...

  3. android app记录执行日志 捕获奔溃异常 ,存储日志到文件

    app在执行过程中.为了后期的维护升级,记录日志是一个很好的方法. 为了读取到app执行时的日志,一般的作法是单独开一个线程,在app执行的启动线程.然后app退出时停掉线程. 然而我们更好的方法是开 ...

  4. 正则基础之——捕获组(capture group)

    1        概述 1.1     什么是捕获组 捕获组就是把正则表达式中子表达式匹配的内容,保存到内存中以数字编号或显式命名的组里,方便后面引用.当然,这种引用既可以是在正则表达式内部,也可以是 ...

  5. mysql基础---日志文件

    一 基本日志文件 MYSQL有不同类型的日志文件(各自存储了不同类型的日志),从它们当中可以查询到MYSQL里都做了些什么,对于MYSQL的管理工作,这些日志文件是不可缺少的. 1.错误日志(The ...

  6. C#基础学习之StreamReader和StreamWriter

    StreamReader和StreamWriter操作字符的 FileStream操作字节的 //使用StreamReader读取文件 using (StreamReader sr=new Strea ...

  7. java基础16 捕获、抛出以、自定义异常和 finally 块(以及关键字:throw 、throws)

    1.异常的体系 /* ------|Throwable:所有异常和错误的超类 ----------|Error(错误):错误一般用于jvm或者硬件引发的问题,所以我们一般不会通过代码去处理错误的 -- ...

  8. 20151024_003_C#基础知识(File / FileStream / StreamReader/StreamWriter)

    1:绝对路径和相对路径 绝对路径:通过给定的路径直接能在我的电脑中找到这个文件. 相对路径:文件相对于应用程序的路径. 2:编码格式 乱码:产生乱码的原因,就是你保存这个文件所采用的编码,跟你打开这个 ...

  9. C#操作日志

    首先引用NLog的dll文件 using System.IO; using NLog; -------------------------------------------------------- ...

随机推荐

  1. python2.* 版本 与 3.* 版本中的区别

    目录 Unicode编码 print函数 raw_input() 和 input( ) 不等运算符 数据类型 除法 map 和 filter Unicode编码 python2.x 解释器默认编码格式 ...

  2. leetcode_二叉树篇_python

    主要是深度遍历和层序遍历的递归和迭代写法. 另外注意:因为求深度可以从上到下去查 所以需要前序遍历(中左右),而高度只能从下到上去查,所以只能后序遍历(左右中). 所有题目首先考虑root否是空.有的 ...

  3. centos7+腾讯云服务器搭建wordpress

    title: centos7+腾讯云服务器搭建wordpress date: 2020-03-04 categories: web tags: [wordpress] 分两部分:1.搭建LEMP环境 ...

  4. 爬虫入门二 beautifulsoup

    title: 爬虫入门二 beautifulsoup date: 2020-03-12 14:43:00 categories: python tags: crawler 使用beautifulsou ...

  5. Error Code: 1055.Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'userinfo.

    环境:mysql-8.0.15-winx64 问题描述: Error querying database. Cause: java.sql.SQLSyntaxErrorException: Expre ...

  6. Google reCAPTCHA 2 : Protect your site from spam and abuse & Google reCAPTCHA 2官方教程

    1

  7. cookie & maxAge & expires

    cookie & maxAge & expires Expires (timestamp) & Max-Age (seconds) https://developer.mozi ...

  8. how to create react custom hooks with arguments

    how to create react custom hooks with arguments React Hooks & Custom Hooks // reusable custom ho ...

  9. gradient text & gradient background

    gradient text & gradient background -webkit-text-fill-color & -webkit-gradient https://wesbo ...

  10. ruby & rvm

    ruby & rvm https://rvm.io/ Ruby Version Manager (RVM) RVM is a command-line tool which allows yo ...