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";

public static readonly string LogFile = RootPath.TrimEnd('\\') + "\\" + FileFormat;

/// <summary>
        /// 默认Trace监听
        /// </summary>
        public static MyTraceListener traceListener ;

/// <summary>
        /// 静态实例
        /// </summary>
        private static readonly Logger instance = new Logger();
        static Logger()
        {
            if (!Directory.Exists(RootPath))
            {
                Directory.CreateDirectory(RootPath);
            }

if (!File.Exists(LogFile))
            {
                File.Create(LogFile).Close();
            }
            traceListener = new MyTraceListener(LogFile);
            Trace.Listeners.Clear();
            Trace.Listeners.Add(traceListener);
            Trace.AutoFlush = true;
        }

/// <summary>
        /// log输出<br>     /// 当然这里可以自己随便扩充
        /// </summary>
        /// <param name="msg"></param>
        public static void Log(string msg)
        {
            traceListener.WriteLine(msg);
        }
        public static void Log(object o, string category)
        {
            traceListener.Write(o, category);
        }
    }

public class MyTraceListener : TraceListener
    {
        public string FilePath { get; private set; }

public MyTraceListener(string filepath)
        {
            FilePath = filepath;
        }
        public override void Write(string message)
        {
            File.AppendAllText(FilePath, message);
        }

public override void WriteLine(string message)
        {
            File.AppendAllText(FilePath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss    ") + message + Environment.NewLine);
        }

public override void Write(object o, string category)
        {
            string msg = "";

if (string.IsNullOrWhiteSpace(category) == false) //category参数不为空
            {
                msg = category + " : ";
            }

if (o is Exception) //如果参数o是异常类,输出异常消息+堆栈,否则输出o.ToString()
            {
                var ex = (Exception)o;
                msg += ex.Message + Environment.NewLine;
                msg += ex.StackTrace;
            }
            else if (o != null)
            {
                msg = o.ToString();
            }

WriteLine(msg);
        }
    }
}

MyLog的更多相关文章

  1. Python3自定义日志类 mylog

    #encoding=utf-8 import os, sysimport datetimeimport time class Mylog(object): # 根文件夹    root_dir = s ...

  2. Membership三步曲之进阶篇 - 深入剖析Provider Model

    Membership 三步曲之进阶篇 - 深入剖析Provider Model 本文的目标是让每一个人都知道Provider Model 是什么,并且能灵活的在自己的项目中使用它. Membershi ...

  3. org.apache.log4j.Logger详解

    org.apache.log4j.Logger 详解 1. 概述 1.1. 背景 在应用程序中添加日志记录总的来说基于三个目的 :监视代码中变量的变化情况,周期性的记录到文件中供其他应用进行统计分析工 ...

  4. Mysql性能优化三(分表、增量备份、还原)

    接上篇Mysql性能优化二 对表进行水平划分 如果一个表的记录数太多了,比如上千万条,而且需要经常检索,那么我们就有必要化整为零了.如果我拆成100个表,那么每个表只有10万条记录.当然这需要数据在逻 ...

  5. (转)配置Log4j(很详细)

    来自:http://blog.csdn.net/yttcjj/article/details/37957317 Log4J的配置文件(Configuration File)就是用来设置记录器的级别.存 ...

  6. MSBuild 编译 C# Solution

    Microsoft(R) 生成引擎版本 4.6.1055.0 [Microsoft .NET Framework 版本 4.0.30319.42000] 版权所有 (C) Microsoft Corp ...

  7. [转]C# 使用Nlog记录日志到数据库

    本文转自:http://www.cnblogs.com/weixing/archive/2013/04/26/3044422.html 摘要]Nlog是一个很不错的.NET日志记录组件,它可以将日志输 ...

  8. Linux下使用crontab定时备份日志

    上周学习了Linux,其中有使用crontab定时备份日志的内容,现把主要步骤记录如下: 首先需要备份的日志的源目录位于/opt/lampp/logs/access_log 备份到/tmp/logs下 ...

  9. logback 常用配置详解<appender>

    logback 常用配置详解 <appender> <appender>: <appender>是<configuration>的子节点,是负责写日志的 ...

随机推荐

  1. HDU 3948 The Number of Palindromes(Manacher+后缀数组)

    题意 求一个字符串中本质不同的回文子串的个数. $ 1\leq |string| \leq 100000$ 思路 好像是回文自动机的裸题,但是可以用 \(\text{Manacher}\) (马拉车) ...

  2. 题解 CF934A 【A Compatible Pair】 ——贪心

    题意: 给定两个数列 \(A\) . \(B\) ,元素个数分别为 \(n\) , \(m\) \((2 \le n,m \le 50)\) .数列中所有元素大小均在 \(-10^{9}\) 到 \( ...

  3. 剑指offer 11:二进制中 1 的个数

    题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 解题代码 法一: public class Solution { public int NumberOf1(int n) { ...

  4. jsp servlet table 集合list 数据 绑定

    删除 前端

  5. Pandas 基础(17) - to_datetime

    这一节依然是关于时间的知识, 在平时的工作中, 有一个非常令我们恼火的就是时间的格式可以有很多种表达, 比如下面这张图, 我们看到同样是 2017年1月5日, 可以有很多种时间的格式, 我们需要先将格 ...

  6. mysql用户管理和pymysql模块

    一:mysql用户管理 mysql是一个tcp的服务器,用于操作服务器上的文件数据,接收用户端发送的指令,而接收指令时就 需要考虑安全问题. 在mysql自带的数据库中有4个表是用于用户管理的,分别是 ...

  7. Left Join on 多条件查询时,条件过滤的问题

    例如:A  Left Join B on (...) on 后面的条件是对B数据的过滤,如果要对A的数据或者联合之后的数据集进行过滤,则要把过滤条件放在where子句中

  8. 《R语言入门与实践》第四章:R 的记号体系

    这一章节将如何对 R 对象中的值进行选取,R 的符号规则有两种方式进行查询: 第一种记号体系:索引查询索引语法:deck[ , ](使用中括号)其中[ , ] 为索引,其中含有两个索引参数,用 &qu ...

  9. 《javascript经典入门》-day02

    <javascript经典入门>-day02 1.使用函数 1.1基本语法 function sayHello() { aler('Hello'); //...其他语句... } #关于函 ...

  10. Lab 9-3

    Analyze the malware found in the file Lab09-03.exe using OllyDbg and IDA Pro. This malware loads thr ...