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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值