log4net之类3方组件确实很方便,但是想写个小小的demo之类的程序,有点用不起啊。

微软自带的TraceListener要实现一个简易的日志帮助类还是很简单的,直接上代码,自己备用,也希望对同样烦恼的哥们有用:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics; namespace TestExpressionLambda
{
public class Logger
{
/// <summary>
/// 默认路径
/// </summary>
public static readonly string RootPath = AppDomain.CurrentDomain.BaseDirectory + "log"; /// <summary>
/// 默认文件名格式
/// </summary>
public static readonly string FileFormat = DateTime.Now.ToString("yyyy-MM-dd") + ".log"; /// <summary>
/// 默认Trace监听
/// </summary>
public static TextWriterTraceListener traceListener; /// <summary>
/// 静态实例
/// </summary>
private static readonly Logger instance = new Logger();
static Logger()
{
if (!Directory.Exists(RootPath))
{
Directory.CreateDirectory(RootPath);
} string logFile = RootPath.TrimEnd('\\') + "\\" + FileFormat;
//if (!File.Exists(logFile))
//{
// File.Create(logFile);
//}
//这里不用create文件,TextWriterTraceListener会自动创建了,谢谢楼下的哥们 Trace.AutoFlush = true;
traceListener = new TextWriterTraceListener(logFile);
Trace.Listeners.Add(traceListener);
} /// <summary>
/// log输出
     /// 当然这里可以自己随便扩充
/// </summary>
/// <param name="msg"></param>
public static void Log(string msg)
{
Trace.WriteLine(msg);
}
}
}

用TextWriterTraceListener实现建议log文件记录的更多相关文章

  1. 用TextWriterTraceListener实现log文件记录 (转载)

    log4net之类3方组件确实很方便,但是想写个小小的demo之类的程序,有点用不起啊. 微软自带的TraceListener要实现一个简易的日志帮助类还是很简单的,直接上代码,自己备用,也希望对同样 ...

  2. leveldb 学习记录(四)Log文件

    前文记录 leveldb 学习记录(一) skiplistleveldb 学习记录(二) Sliceleveldb 学习记录(三) MemTable 与 Immutable Memtablelevel ...

  3. SQLServer2005删除log文件和清空日志的方案

    数据库在使用过程中会使日志文件不断增加,使得数据库的性能下降,并且占用大量的磁盘空间.SQL Server数据库都有log文件,log文件记录用户对数据库修改的操作.可以通过直接删除log文件和清空日 ...

  4. SQL SERVER数据库删除LOG文件和清空日志的方案

    原文:SQL SERVER数据库删除LOG文件和清空日志的方案 数据库在使用过程中会使日志文件不断增加,使得数据库的性能下降,并且占用大量的磁盘空间.SQL Server数据库都有log文件,log文 ...

  5. [转]SQL SERVER数据库删除LOG文件和清空日志的方案

    本文转自:https://www.cnblogs.com/ShaYeBlog/archive/2012/09/04/2670505.html 数据库在使用过程中会使日志文件不断增加,使得数据库的性能下 ...

  6. SQLServer:删除log文件和清空日志的方法

    数据库的性能是DBA都需要重点关注的,日志文件的增多严重影响数据库的性能,本文将为您介绍SQL Server删除日志文件的方法,供您参考,希望对您有所帮助. 数据库在使用过程中会使日志文件不断增加,使 ...

  7. 用户收到"无法显示页面"的错误消息和"Connections_refused"条目记录在运行 Windows Server 2003,Exchange 2003 和 IIS 6.0 的服务器上的 Httperr.log 文件

    症状 您会遇到下列症状在运行 Microsoft Windows Server 2003. Microsoft Exchange Server 2003年和 Microsoft Internet In ...

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

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

  9. (转)Unity3D研究院之IOS&Android收集Log文件

    转自:http://www.xuanyusong.com/archives/2477 有段时间没有写过文章了,不知道大伙儿还记得雨松MOMO吗? 嘿嘿. 开发项目的时候尤其在处理与服务器交互这块,如果 ...

随机推荐

  1. Katalon Recorder 自动录制 Selenium 爬虫脚本

    相信很多小伙伴都用过 Selenium 来完成爬虫工作,今天就给大家带来一个神器,可以录制你的浏览器动作,然后直接生成 Selenium 脚本,是不是心动了? 1 Selenium 简介 Seleni ...

  2. 洛谷P2983 [USACO10FEB]购买巧克力Chocolate Buying

    题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Sto ...

  3. 【C++基础】形参实参

    c++规定:一个函数的默认实参既可以在定义中,也可在声明中指定,但在一个文件(准确的说,是一个作用域)中只能为一个形参指定默认实参一次

  4. 【LeetCode】047. Permutations II

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  5. django 自定义日志配置

    如果不想使用 python 的 dictConfig 格式来配置 logger,可以制定自己的配置架构. LOGGING_CONFIG 配置定义了用来配置 django logger 的可调用函数,默 ...

  6. Python基础入门知识

    本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语 ...

  7. BZOJ3165:[HEOI2013]Segment

    浅谈标记永久化:https://www.cnblogs.com/AKMer/p/10137227.html 题目传送门:https://www.lydsy.com/JudgeOnline/proble ...

  8. Spring管理Filter和Servlet(在servlet中注入spring容器中的bean)

    在使用spring容器的web应用中,业务对象间的依赖关系都可以用context.xml文件来配置,并且由spring容器来负责依赖对象 的创建.如果要在servlet中使用spring容器管理业务对 ...

  9. XCode工程中 Project 和 Targets区别

    转自:http://blog.csdn.net/zhaozy55555/article/details/8557175 project就是一个项目,或者说工程,一个project可以对应多个targe ...

  10. 常量指针-指向常量的指针,指针常量-指针本身是常量,常量-不能更改值的常量,数组指针-是指针int (*p)[n] 指针数组-是数组int *p[n]

    1.常量指针 定义:具有只能够读取内存中数据,却不能够修改内存中数据的属性的指针,称为指向常量的指针,简称常量指针. 声明:const int * p; int const * p; 注:可以将一个常 ...