C#日志记录类
public class WriteLog
{
/// <summary>
/// 将错误写入文件中
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="exception">发生的异常</param>
public static void WriteErorrLog(string fileName, Exception exception)
{
if (exception == null) return; //ex = null 返回
DateTime dt = DateTime.Now; // 设置日志时间
string time = dt.ToString("yyyy-MM-dd HH:mm:ss"); //年-月-日 时:分:秒
string logName = dt.ToString("yyyy-MM-dd"); //日志名称
string logPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Path.Combine("log", fileName)); //日志存放路径
string log = Path.Combine(logPath, string.Format("{0}.log", logName)); //路径 + 名称
try
{
FileInfo info = new FileInfo(log);
if (info.Directory != null && !info.Directory.Exists)
{
info.Directory.Create();
}
using (StreamWriter write = new StreamWriter(log, true, Encoding.GetEncoding("utf-8")))
{
write.WriteLine(time);
write.WriteLine(exception.Message);
write.WriteLine("异常信息:" + exception);
write.WriteLine("异常堆栈:" + exception.StackTrace);
write.WriteLine("异常简述:" + exception.Message);
write.WriteLine("\r\n----------------------------------\r\n");
write.Flush();
write.Close();
write.Dispose();
}
}
catch { }
}
/// <summary>
/// 将终端内容打印到文件中
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="message">所要写入的内容</param>
public static bool WriteMessage(string fileName, string message)
{
//ex = null 返回
DateTime dt = DateTime.Now; // 设置日志时间
string time = dt.ToString("yyyy-MM-dd HH:mm:ss"); //年-月-日 时:分:秒
string logName = dt.ToString("yyyy-MM-dd"); //日志名称
string logPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Path.Combine("log", fileName)); //日志存放路径
string log = Path.Combine(logPath, string.Format("{0}.log", logName)); //路径 + 名称
try
{
FileInfo info = new FileInfo(log);
if (info.Directory != null && !info.Directory.Exists)
{
info.Directory.Create();
}
using (StreamWriter write = new StreamWriter(log, true, Encoding.GetEncoding("utf-8")))
{
write.WriteLine(time);
write.WriteLine("信息:" + message);
write.WriteLine("\r\n----------------------------------\r\n");
write.Flush();
write.Close();
write.Dispose();
}
return true;
}
catch (Exception e)
{
WriteErorrLog("WriteMessageException", e);
return false;
}
}
/// <summary>
/// 将错误写入文件中
/// </summary>
/// <param name="exception">发生的错误</param>
/// <param name="message">需要写入的消息</param>
public static bool WriteErorrLog(Exception exception, string message)
{
if (exception == null) return false; //ex = null 返回
DateTime dt = DateTime.Now; // 设置日志时间
string time = dt.ToString("yyyy-MM-dd HH:mm:ss"); //年-月-日 时:分:秒
string logName = dt.ToString("yyyy-MM-dd"); //日志名称
string logPath = System.AppDomain.CurrentDomain.BaseDirectory; //日志存放路径
string log = Path.Combine(Path.Combine(logPath, "log"), string.Format("{0}.log", logName)); //路径 + 名称
try
{
FileInfo info = new FileInfo(log);
if (info.Directory != null && !info.Directory.Exists)
{
info.Directory.Create();
}
using (StreamWriter write = new StreamWriter(log, true, Encoding.GetEncoding("utf-8")))
{
write.WriteLine(time);
write.WriteLine(exception.Message);
write.WriteLine("异常信息:" + exception);
write.WriteLine("异常堆栈:" + exception.StackTrace);
write.WriteLine("异常简述:" + message);
write.WriteLine("\r\n----------------------------------\r\n");
write.Flush();
write.Close();
write.Dispose();
}
return true;
}
catch (Exception e)
{
WriteMessage("ErrorLogException", e.ToString());
return false;
}
}
/// <summary>
/// 将消息写入文件
/// </summary>
/// <param name="message">需要写入的内容</param>
public static bool WriteMessage(string message)
{
//ex = null 返回
DateTime dt = DateTime.Now; // 设置日志时间
string time = dt.ToString("yyyy-MM-dd HH:mm:ss"); //年-月-日 时:分:秒
string logName = dt.ToString("yyyy-MM-dd"); //日志名称
string logPath = System.AppDomain.CurrentDomain.BaseDirectory; //日志存放路径
// System.Console.WriteLine(logPath);
string log = Path.Combine(Path.Combine(logPath, "log"), string.Format("{0}.log", logName)); //路径 + 名称
try
{
FileInfo info = new FileInfo(log);
if (info.Directory != null && !info.Directory.Exists)
{
info.Directory.Create();
}
using (StreamWriter write = new StreamWriter(log, true, Encoding.GetEncoding("utf-8")))
{
write.WriteLine(time);
write.WriteLine("信息:" + message);
write.WriteLine("\r\n----------------------------------\r\n");
write.Flush();
write.Close();
write.Dispose();
}
return true;
}
catch (Exception e)
{
WriteErorrLog("WriteMessageException", e);
return false;
}
}
}
作者:艾孜尔江
C#日志记录类的更多相关文章
- 【个人使用.Net类库】(2)Log日志记录类
开发接口程序时,要保证程序稳定运行就要时刻监控接口程序发送和接收的数据,这就需要一个日志记录的类将需要的信息记录在日志文件中,便于自己维护接口程序.(Web系统也是如此,只是对应的日志实现比这个要复杂 ...
- 日志记录类LogHelper
开源日志log4net使用起来很方便,但是项目中不让用,所以自己重写了一个类,用来记录日志,比较简单. 1.首先是可以把日志分成多个类型,分别记录到不同的文件中 /// <summary> ...
- 【C#通用类】日志记录类
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...
- 利用AOP与ToStringBuilder简化日志记录
刚学spring的时候书上就强调spring的核心就是ioc和aop blablabla...... IOC到处都能看到...AOP么刚开始接触的时候使用在声明式事务上面..当时书上还提到一个用到ao ...
- 将错误日志记录在txt文本里
引言 对于已经部署的系统一旦出错对于我们开发人员来说是比较痛苦的事情,因为我们不能跟踪到错误信息,不能 很快的定位到我们的错误位置在哪,这时候如果能像开发环境一样记录一些堆栈信息就可以了,这时候我们就 ...
- Spring AOP进行日志记录
在java开发中日志的管理有很多种.我一般会使用过滤器,或者是Spring的拦截器进行日志的处理.如果是用过滤器比较简单,只要对所有的.do提交进行拦截,然后获取action的提交路径就可以获取对每个 ...
- Java 基于log4j的日志工具类
对log4j日志类进行了简单封装,使用该封装类的优势在于以下两点: 1.不必在每个类中去创建对象,直接类名 + 方法即可 2.可以很方便的打印出堆栈信息 package com.tradeplatfo ...
- Spring AOP进行日志记录,管理
在java开发中日志的管理有很多种.我一般会使用过滤器,或者是Spring的拦截器进行日志的处理.如果是用过滤器比较简单,只要对所有的.do提交进行拦截,然后获取action的提交路径就可以获取对每个 ...
- Asp.Net Core 2.0 项目实战(9) 日志记录,基于Nlog或Microsoft.Extensions.Logging的实现及调用实例
本文目录 1. Net下日志记录 2. NLog的使用 2.1 添加nuget引用NLog.Web.AspNetCore 2.2 配置文件设置 2.3 依赖配置及调用 ...
随机推荐
- Vue项目中实现图片懒加载
个人网站 https://iiter.cn 程序员导航站 开业啦,欢迎各位观众姥爷赏脸参观,如有意见或建议希望能够不吝赐教! ---对于图片过多的页面,为了加速页面加载速度,所以很多时候我们需要将页面 ...
- log4net进阶手札(二):基本用法
本节将主要在WebSite中,对保存日志在文本文件的基本用法来进行介绍,并结合WebForm的初始化方式区别进行说明,解决方案如下图所示: 一.WebSite应用第1步:配置Web.Config文件, ...
- Centos7网络配置( 网关、dns、ip地址配置)
0.前提:设置VMware的虚拟网络编辑器 子网和网关设置 虚拟网络编辑器 1.配置DNS(可忽略) $vim /etc/resolv.conf nameserver 192.168.0.1 2. ...
- RF(自定义关键字)
1.在 D:\work_software\python\Lib\site-packages 文件夹下, 新建 python package 包 ,例如我的是 TestLibrary 建好后的完整路径: ...
- 在for循环里面的++i与i++的区别
++i与i++在表面上没有什么区别 for(语句 1;语句 2;语句 3) 语句 1 在循环(代码块)开始前执行 语句 2 定义运行循环(代码块)的条件 语句 3 在循环(代码块)已被执行之后执行 ( ...
- 题目分享R
题意:有n只蚂蚁在木棍上爬行,每只蚂蚁的速度都是每秒1单位长度,现在给你所有蚂蚁初始的位置(蚂蚁运动方向未定),蚂蚁相遇会掉头反向运动,让你求出所有蚂蚁都·掉下木棍的最短时间和最长时间. 分析:(其实 ...
- TestNG测试用例重跑详解及实践优化
测试用例运行稳定性是自动化质量的一个重要指标,在运行中需要尽可能的剔除非bug造成的测试用例执行失败,对于失败用例进行重跑是常用策略之一.一种重跑策略是所有用例运行结束后对失败用例重跑,另一种重跑策略 ...
- spring的后台数据校验
数据校验对于开发项目来说是必须的.校验一般分为前台校验和后台校验,前台校验是必须要做的,后台校验是可选的.后台校验相对前台校验来说配置起来一般更复杂.前台校验通过js做,前台校验一般非常容易绕过.sp ...
- Java常用关键字总结
1.abstract abstract修饰类,表示抽象的意思,抽象类可以含有非抽象变量和成员变量,也可以有普通方法和构造方法,但是不能被实例化(接口),但是可以被子类继承. public abstra ...
- 自定Tinymce中的图片上传
在引入组件上添加 上传图片的url地址 <tinymce :height="300" ref="tinymce" //上传图片的url地址 upload- ...