zoukankan      html  css  js  c++  java
  • 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("
    归属模块:".$module."
    ");}
        $logLine = "
    -------------------------------  $time -------------------------------
    ";
        $logLine .= $module;
        $logLine .= "
    异常信息:$msg
    ";
        $logLine .= "
    错误等级:$logLevel
    ";
        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("调用堆栈:
    " + 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();
     
                }
            }
     
     
        }
    }
     
     
  • 相关阅读:
    基于visual c++之windows核心编程代码分析(33)实现防火墙模型
    基于visual c++之windows核心编程代码分析(31)SNMP协议编程
    未来的职业?
    关于 Delphi 中流的使用(8) 压缩与解压缩的函数
    Delphi 中的 XMLDocument 类详解(2) 记要
    Delphi 中的 XMLDocument 类详解(1) 等待研究的内容
    关于 Delphi 中流的使用(5) 组件序列化
    关于 Delphi 中流的使用(6) 用流读写结构化文件
    xml 语法提示
    关于 Delphi 中流的使用(4) 遍历读取流中的所有数据
  • 原文地址:https://www.cnblogs.com/CyLee/p/5324666.html
Copyright © 2011-2022 走看看