首页 > Windows开发 > 详细

C# - 日志类

时间:2016-03-27 01:40:26      阅读:280      评论:0      收藏:0      [点我收藏+]

简易的txt日志类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
 
 
namespace Lee
{
    public class log
    {
        /// <summary>
        /// 将异常打印到LOG文件
        /// </summary>
        /// <param name="ex">异常</param>
        /// <param name="LogAddress">日志文件地址</param>
        public static void WriteLog(Exception ex, string say)
        {
            //如果日志文件为空,则默认在Debug目录下新建 YYYY-mm-dd_Log.log文件
 
            string LogAddress = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "logs" + "\\";
 
 
            if (!System.IO.Directory.Exists(LogAddress))
            {
                System.IO.Directory.CreateDirectory(LogAddress);//不存在就创建目录
 
            }
 
 
 
            LogAddress += DateTime.Now.Year + "-" +
                          DateTime.Now.Month + "-" +
                          DateTime.Now.Day + "_Log.log";
 
            //把异常信息输出到文件
            StreamWriter fs = null;
            try
            {
                fs = new StreamWriter(LogAddress, true);
                fs.WriteLine("当前时间:" + DateTime.Now.ToString());
                fs.WriteLine("异常信息:" + ex.Message);
                fs.WriteLine("异常对象:" + ex.Source);
                fs.WriteLine("调用堆栈:\n" + ex.StackTrace.Trim());
                fs.WriteLine("触发方法:" + ex.TargetSite);
                fs.WriteLine("留言:" + say);
                fs.WriteLine();
                fs.Close();
            }
            catch
            {
                if (fs != null)
                    fs.Close();
 
            }
        }
 
        public static void WriteLog(string say)
        {
            //如果日志文件为空,则默认在Debug目录下新建 YYYY-mm-dd_Log.log文件
 
            string LogAddress = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "logs" + "\\";
 
 
            if (!System.IO.Directory.Exists(LogAddress))
            {
                System.IO.Directory.CreateDirectory(LogAddress);//不存在就创建目录
 
            }
 
            LogAddress += DateTime.Now.Year + "-" +
                          DateTime.Now.Month + "-" +
                          DateTime.Now.Day + "_Log.log";
 
            //把异常信息输出到文件
            StreamWriter fs = null;
            try
            {
                fs = new StreamWriter(LogAddress, true);
                fs.WriteLine("当前时间:" + DateTime.Now.ToString());
                fs.WriteLine("留言:" + say);
                fs.WriteLine();
                fs.Close();
            }
            catch
            {
                if (fs != null)
                    fs.Close();
 
            }
        }
 
 
 
 
 
 
 
        public static void WriteHuaKouLog(string say)
        {
            //如果日志文件为空,则默认在Debug目录下新建 YYYY-mm-dd_Log.log文件
 
            string LogAddress = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "划扣申请日志" + "\\";
 
 
            if (!System.IO.Directory.Exists(LogAddress))
            {
                System.IO.Directory.CreateDirectory(LogAddress);//不存在就创建目录
 
            }
 
            LogAddress += DateTime.Now.Year + "-" +
                          DateTime.Now.Month + "-" +
                          DateTime.Now.Day + "_Log.log";
 
            //把异常信息输出到文件
            StreamWriter fs = null;
            try
            {
                fs = new StreamWriter(LogAddress, true);
                fs.WriteLine("当前时间:" + DateTime.Now.ToString());
                fs.WriteLine("留言:" + say);
                fs.WriteLine();
                fs.Close();
            }
            catch
            {
                if (fs != null)
                    fs.Close();
 
            }
        }
 
 
    }
}
 
 

 

C# - 日志类

原文:http://www.cnblogs.com/CyLee/p/5324666.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!