C# 基础 - 日志捕获一使用 StreamWriter
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的更多相关文章
- C# 基础 - 日志捕获二使用 log4net
引入 log4net.dll 项目->添加->新建项->应用程序配置文件,命名为 log4net.config,并把属性的复制到输出目录设置为 如果较新则复制,后续客户端需要读取在 ...
- Logan:美团点评的开源移动端基础日志库
前言 Logan是美团点评集团移动端基础日志组件,这个名称是Log和An的组合,代表个体日志服务.同时Logan也是“金刚狼”大叔的名号,当然我们更希望这个产品能像金刚狼大叔一样犀利. Logan已经 ...
- android app记录执行日志 捕获奔溃异常 ,存储日志到文件
app在执行过程中.为了后期的维护升级,记录日志是一个很好的方法. 为了读取到app执行时的日志,一般的作法是单独开一个线程,在app执行的启动线程.然后app退出时停掉线程. 然而我们更好的方法是开 ...
- 正则基础之——捕获组(capture group)
1 概述 1.1 什么是捕获组 捕获组就是把正则表达式中子表达式匹配的内容,保存到内存中以数字编号或显式命名的组里,方便后面引用.当然,这种引用既可以是在正则表达式内部,也可以是 ...
- mysql基础---日志文件
一 基本日志文件 MYSQL有不同类型的日志文件(各自存储了不同类型的日志),从它们当中可以查询到MYSQL里都做了些什么,对于MYSQL的管理工作,这些日志文件是不可缺少的. 1.错误日志(The ...
- C#基础学习之StreamReader和StreamWriter
StreamReader和StreamWriter操作字符的 FileStream操作字节的 //使用StreamReader读取文件 using (StreamReader sr=new Strea ...
- java基础16 捕获、抛出以、自定义异常和 finally 块(以及关键字:throw 、throws)
1.异常的体系 /* ------|Throwable:所有异常和错误的超类 ----------|Error(错误):错误一般用于jvm或者硬件引发的问题,所以我们一般不会通过代码去处理错误的 -- ...
- 20151024_003_C#基础知识(File / FileStream / StreamReader/StreamWriter)
1:绝对路径和相对路径 绝对路径:通过给定的路径直接能在我的电脑中找到这个文件. 相对路径:文件相对于应用程序的路径. 2:编码格式 乱码:产生乱码的原因,就是你保存这个文件所采用的编码,跟你打开这个 ...
- C#操作日志
首先引用NLog的dll文件 using System.IO; using NLog; -------------------------------------------------------- ...
随机推荐
- Redis的主从架构+哨兵模式
Redis主从架构 redis主从架构搭建,配置从节点步骤: 1.复制一份redis.conf文件的目录 cd /usr/local/java cp -a redis redis_6380 2.将相关 ...
- 从.NET看微软的焦虑
节日没事,就像聊聊微软的NET. 1.孩子静悄悄,必定在作妖 截止目前,微软的市值达到1.85万亿美元,按说,这样一个宙斯级的巨无霸应该过的非常舒坦, 但是,和微软市值成鲜明的反差,我们从.NET的发 ...
- markdown table collapse span
markdown table collapse span refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问! 原创文章,版权所有️x ...
- Vue Component Registration All In One
Vue Component Registration All In One Vue 注册自定义组件 <template> <div class="back-to-top-c ...
- 浏览器缓存 All In One
浏览器缓存 All In One HTTP 缓存 强缓存 expired Cache-Control max-age s-maxage 协商缓存 E-tag last-modified 本地缓存 co ...
- TypeScript 4.0 New Features
TypeScript 4.0 New Features $ npm install typescript@beta https://devblogs.microsoft.com/typescript/ ...
- vue 使用stylus
λ yarn add stylus stylus-loader --dev <style scoped lang="stylus"> div color #ff4488 ...
- go-admin在线开发平台学习-4[细节解析]
紧接着上一期的内容,继续对go-admin的一些细节进行解析. 通用的增删改查方法 在go-admin/common/actions下封装了通用的增删改查方法.在对这些方法进行说明前,先移步到另一个文 ...
- 磁盘使用率/文件大小查看指南du & df
一.前言 磁盘使用率,文件大小查看是我们日常使用命令.这两个是配合使用的,磁盘使用率过高告警了,那么得找到对应的磁盘(df),然后找到对应磁盘下的哪个目录和文件占用了空间(du). df(Disk f ...
- Linux 内核和 Windows 内核有什么区别?
Windows 和 Linux 可以说是我们比较常见的两款操作系统的. Windows 基本占领了电脑时代的市场,商业上取得了很大成就,但是它并不开源,所以要想接触源码得加入 Windows 的开发团 ...