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. C++获取当前系统时间并格式化输出

    C++中与系统时间相关的函数定义在头文件中. 一.time(time_t * )函数 函数定义如下: time_t time (time_t* timer); 获取系统当前日历时间 UTC 1970- ...

  2. javascript阻止子元素继承父元素事件

    $('.box').on('click', function (e) { if(e.target == this) { console.log(e.target) } })

  3. ubuntu 和 centos 如何区分系统

    Ubuntu Ubuntu有着漂亮的用户界面,完善的包管理系统,强大的软件源支持,丰富的技术社区,Ubuntu还对大多数硬件有着良好的兼容性,包括最新的图形显卡等等.这一切让Ubuntu越来越向大众化 ...

  4. JVM系列-2、JVM内存结构

    一.JVM内存结构 1.1.栈(JVM Stacks) 存放局部变量(定义在方法中的变量和定义在方法参数列表上的变量).对象引用(reference类型,它不等同于对象本身,根据不同的虚拟机实现,它可 ...

  5. Oracle的三种循环

    一.loop循环 语法:声明循环变量loop判断循环条件 ,如果循环条件不成立,跳出循环if 条件表达式 thenexit;end if;语句块;改变循环变量的值end loop; 举例:输出1到10 ...

  6. ActiveMQ 事务、集群、持久订阅者、ActiveMQ监控

    JMS介绍 JMS是什么? JMS的全称Java Message Service,既Java消息服务. JMS是SUN提供的旨在统一各种MOM(Message-Oriented Middleware) ...

  7. 【Scala】Actor并发编程实现单机版wordCount

    文章目录 对单个文本文件进行单词计数 对多个文本文件进行单词计数 对单个文本文件进行单词计数 import scala.actors.Actor import scala.io.Source //读取 ...

  8. 微信小程序使用GoEasy实现websocket实时通讯

    不需要下载安装,便可以在微信好友.微信群之间快速的转发,用户只需要扫码或者在微信里点击,就可以立即运行,有着近似APP的用户体验,使得微信小程序成为全民热爱的好东西~ 同时因为微信小程序使用的是Jav ...

  9. 浅析Spring中bean的作用域

    一.前言   刚刚花了点时间,阅读了一下Spring官方文档中,关于bean的作用域这一块的内容.Spring-4.3.21官方文档中,共介绍了七种bean作用域,这篇博客就来简单介绍一下这七种作用域 ...

  10. JAVA知识总结(四):单例模式和多态

    好吧,今天一定要把面向对象的最后一个特性:多态,给说完.不过我们先来聊一聊设计模式,因为它很重要. 设计模式 官方的解释是,设计模式是:一套被反复使用,多数人知晓的,经过分类编目,代码设计经验的总结. ...