C# - 日志类

函数版本:

//创建文件夹
function mkFolder($path)
{
    if(!is_readable($path))  is_file($path) or mkdir($path,'0777');
}

//调试日志
function WriteLog($msg,$module = null,$logLevel = "DEBUG")
{
    $filepath = "Log/";
    mkFolder($filepath);
    $MyLogFile = @fopen($filepath.date("Y-m-d").".txt",'a+');
    $time = date("Y-m-d H:i:s");
    if(isset($module)){$module =  sprintf("\r\n归属模块:".$module."\r\n");}
    $logLine = "\r\n-------------------------------  $time -------------------------------\r\n";
    $logLine .= $module;
    $logLine .= "\r\n异常信息:$msg\r\n";
    $logLine .= "\r\n错误等级:$logLevel\r\n";
    fwrite($MyLogFile,$logLine);
}

 

简易的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();
 
            }
        }
 
 
    }
}
 
 

 

posted @ 2016-03-27 00:47  贝尔塔猫  阅读(379)  评论(0编辑  收藏  举报