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#日志记录类的更多相关文章

  1. 【个人使用.Net类库】(2)Log日志记录类

    开发接口程序时,要保证程序稳定运行就要时刻监控接口程序发送和接收的数据,这就需要一个日志记录的类将需要的信息记录在日志文件中,便于自己维护接口程序.(Web系统也是如此,只是对应的日志实现比这个要复杂 ...

  2. 日志记录类LogHelper

    开源日志log4net使用起来很方便,但是项目中不让用,所以自己重写了一个类,用来记录日志,比较简单. 1.首先是可以把日志分成多个类型,分别记录到不同的文件中 /// <summary> ...

  3. 【C#通用类】日志记录类

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  4. 利用AOP与ToStringBuilder简化日志记录

    刚学spring的时候书上就强调spring的核心就是ioc和aop blablabla...... IOC到处都能看到...AOP么刚开始接触的时候使用在声明式事务上面..当时书上还提到一个用到ao ...

  5. 将错误日志记录在txt文本里

    引言 对于已经部署的系统一旦出错对于我们开发人员来说是比较痛苦的事情,因为我们不能跟踪到错误信息,不能 很快的定位到我们的错误位置在哪,这时候如果能像开发环境一样记录一些堆栈信息就可以了,这时候我们就 ...

  6. Spring AOP进行日志记录

    在java开发中日志的管理有很多种.我一般会使用过滤器,或者是Spring的拦截器进行日志的处理.如果是用过滤器比较简单,只要对所有的.do提交进行拦截,然后获取action的提交路径就可以获取对每个 ...

  7. Java 基于log4j的日志工具类

    对log4j日志类进行了简单封装,使用该封装类的优势在于以下两点: 1.不必在每个类中去创建对象,直接类名 + 方法即可 2.可以很方便的打印出堆栈信息 package com.tradeplatfo ...

  8. Spring AOP进行日志记录,管理

    在java开发中日志的管理有很多种.我一般会使用过滤器,或者是Spring的拦截器进行日志的处理.如果是用过滤器比较简单,只要对所有的.do提交进行拦截,然后获取action的提交路径就可以获取对每个 ...

  9. Asp.Net Core 2.0 项目实战(9) 日志记录,基于Nlog或Microsoft.Extensions.Logging的实现及调用实例

    本文目录 1. Net下日志记录 2. NLog的使用     2.1 添加nuget引用NLog.Web.AspNetCore     2.2 配置文件设置     2.3 依赖配置及调用     ...

随机推荐

  1. Netty(一):ByteBuf读写过程图解

    我们知道ByteBuf通过读写两个索引分离,避免了NIO中ByteBuffer中读写模式切换时,需要flip等繁琐的操作. 今天就通过一段测试代码以及图例来直观的了解下ByteBuf中的readInd ...

  2. EntityFramework 迁移遇到的问题

    2019独角兽企业重金招聘Python工程师标准>>> 添加迁移add-migration migrationname 更新数据Update-Database EntityFrame ...

  3. 初次认识Ngnix

    这几天写那个HTTPD,最后觉得学到的好像很少,而且就算最后运行成功了好像也没有什么.所以想学习一些生活中真正在运用着的web服务器, 上网查询了些资料了解到apache, lighttpd, ngi ...

  4. ES[7.6.x]学习笔记(六)分析器

    在前面的章节中,我们给大家介绍了索引中的映射类型,也就是每一个字段都有一个类型,比如:long,text,date等.这和我们的数据库非常的相似,那么它的不同之处是什么呢?对了,就是全文索引,在ES当 ...

  5. PI/PO Token配置

    接收方通道配置 因为本例中需要在访问业务接口时,获取某平台的TOKEN认证,并在调用业务接口时,将TOKEN一同传给某平台,但是不能放在请求参数中,而是放在HTTP的Headers 注意!是Heade ...

  6. STM32 串口USART DMA方式发送接收数据

    硬件:stm32f103cbt6 软件:STM32F10x_StdPeriph_Lib_V3.5.0 文章目录 头文件 USART3_DR的地址 DMA的通道 DMA的中断 USART接收回调函数 头 ...

  7. CodeForces-650B Image Preview 二分+模拟

    CodeForces-650B Image Preview 题意 手机里有n张图片,打开相机出现的是第一张,第一张右滑得到第n张,同理第n张左滑得到第1张,翻页耗费a秒,看照片耗费1s,但是照片有横屏 ...

  8. 【hdu1006】解方程

    http://acm.hdu.edu.cn/showproblem.php?pid=1006 这题坑了我好久,发现居然是一个除法变成了整除,TAT,所以建议在写较长的运算表达式的时候出现了除法尽量加个 ...

  9. 浅析微软的网关项目 -- ReverseProxy

    浅析微软的网关项目 ReverseProxy Intro 最近微软新开了一个项目 ReverseProxy ,也叫做 YARP(A Reverse Proxy) 官方介绍如下: YARP is a r ...

  10. 将微服务运行在docker上遇到的问题一

    按照类似这样的流程: 但是去访问本机的 localhost:92 localhost:80 都没有任何的内容..... 这是什么原因? 重新再来一次 新写了一个微服务demo jar包: 相应的Doc ...