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 依赖配置及调用 ...
随机推荐
- Centos7网络配置( 网关、dns、ip地址配置)
0.前提:设置VMware的虚拟网络编辑器 子网和网关设置 虚拟网络编辑器 1.配置DNS(可忽略) $vim /etc/resolv.conf nameserver 192.168.0.1 2. ...
- 从零开始配置webpack(基于webpack 4 和 babel 7版本)
webpack 核心概念: Entry: 入口 Module:模块,webpack中一切皆是模块 Chunk:代码库,一个chunk由十多个模块组合而成,用于代码合并与分割 Loader:模块转换器, ...
- Pthon学习相关
1. Cython入门教程:https://www.jianshu.com/p/cfcc2c04a6f5
- POJ3255(次最短路)
描述 求1到n的次最短路 开个\(dis[maxn][2]\)的储存距离的二维数组,0储存最短路,1储存次短路 初始化全为正无穷,\(dis[1][0]=0;\) 然后遍历更新时,先尝试更新最短路和次 ...
- E. Height All the Same
E. Height All the Same 题目大意: 给你一个n*m 的矩阵,让你在里面填数字代表这个位置的高度,数字的范围是[L,R],有两种操作,第一种就是给一位置的高度加2,第二种就是给两个 ...
- 大富翁 线段树+二分 +dfs
https://csustacm.fun/problem/2033 这个题目还是比较简单的,但是比赛的时候没有像清楚,用了一个不太熟悉的数据结构主席树, 所以出现了bug,主席树的bug是真的难找. ...
- Java中的内存
目录 栈(Stack):存放的都是方法中的局部变量.方法的运行一定要在栈当中. 堆(Heap):凡是new出来的东西,都在堆内存当中 方法区(Method Area):存储.class相关信息,包含方 ...
- 【Hadoop离线基础总结】Hadoop的架构模型
Hadoop的架构模型 1.x的版本架构模型介绍 架构图 HDFS分布式文件存储系统(典型的主从架构) NameNode:集群当中的主节点,主要用于维护集群当中的元数据信息,以及接受用户的请求,处理用 ...
- Vue3语法快速入门以及写一个倒计时组件
Vue3写一个倒计时组件 vue3 beta版本发布已有一段时间了,文档也大概看了一下,不过对于学一门技术,最好的方法还是实战,于是找了一个比较简单的组件用vue3来实现,参考的是vant的count ...
- 题解 P2421 【[NOI2002]荒岛野人】
我的第一道数论紫题 首先,我们先看两个野人,他们相遇的充要条件是 \(C_i+P_i\times k\equiv C_j+P_j\times k\;(mod\;M)\) 其中\(k\)是第几年,且\( ...