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 依赖配置及调用 ...
随机推荐
- C#读写ini
using System; using System.IO; using System.Runtime.InteropServices; using System.Text; namespace ...
- Top 命令数据分析
一.top 命令详解 当前时间 20:27:12 当前系统运行时间 3:18秒 1个用户 系统负载平均长度为 0.00,0.00,0.00(分别为1分钟.5分钟.15分钟前到现在的平均值) 第二行为进 ...
- MySQL命令2
索引与外键 // 添加索引 ALTER TABLE orders ADD KEY order_ix_custid(cust_id); // 删除索引 ALTER TABLE orders DROP K ...
- spring boot项目中无法访问resources文件夹问题
如图,浏览器默认访问static文件下的内容,无法访问templates文件下的html文件. 解决方法: 在application.properties文件中添加静态资源目录的配置即可.
- python学习之列表的定义以及增删改查
列表定义: >>> name['lily','lucy','tom'] >>> nums = [11,22,33,'100','lily'] #python中的列表 ...
- 基于胜率矩阵的PageRank排序
在做博弈模型评估的时候,遇到一个问题是如何评价多个模型的优劣.例如我有训练好的三个围棋模型A,B,C,两两之间对打之后有一个胜负关系,如何对这三个模型进行排序呢?通常对于人类选手这种水平有波动的情 ...
- webpack 和 webpack-cli 安装和使用中出现的问题
因为国内防火墙的原因,建议首先安装 cnpm: 使用 npm install cnpm -g 或者 npm install -g cnpm --registry=https://registry.np ...
- 用PHP获取网页上的信息相对于xpath效率低点
用php实现对网页的抓取,及信息的收集,其实就是爬数据,具体实现步骤如下,首先应引入两个文件curl_html_get.php和save_file.php文件,两个文件具体代码是这样的curl_htm ...
- 一看就懂的Ubuntu系统下samba服务器安装配置教程
文章目录 前言 环境搭建 安装 配置 Examples 1 创建共享(任何人都可以访问) 2 单用户权限(需要密码访问) 添加samba用户 配置参数 3 支持游客访问(单用户拥有管理员权限) 前言 ...
- Vue 做的项目在IE下面打开一片空白解决方法
ie浏览器打开报这个错并且页面空白: 原因:这是因为浏览器对于部分ES6语法识别不出来导致,所以我们要安装一个插件,把ES6语法转换成IE浏览器可以识别的语法. 解决方法: 需要安装 "ba ...