web中如何写log日志

摘要:
注意:在正常情况下,我们经常遇到系统错误时,我们不知道错误发生在哪里。此时,我们需要将错误信息记录到文件中,并应用:cs code:usingSystem;使用系统。IO;使用系统。数据使用系统。配置使用系统。Web;///////LogWrite摘要描述////publicclass

说明:

在平时,我们经常会遇到,系统出错的时候,不知道错误出在什么地方,这时候就需要把错误信息记录到到一个文件中

应用:

cs代码:

using System;                                                
using System.IO;                                                
using System.Data;                                                
using System.Configuration;                                                
using System.Web;                                                
                                                
/// <summary>                                                
/// LogWrite 的摘要说明                                                
/// </summary>                                                
public class LogWrite                                                
{                                                
    
/*******************************************************************************************                                                
     * 实例化对象,LogWrite为程序名;                                                
     * private LogWrite lg = new LogWrite("[类名]");                                                
     * 得到程序所在的路径                                                
     LogWrite.serPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; ;                                                
     写日志                                                
     info.WriteDebugLog(methodName,ex.ToString());                                                 
    *******************************************************************************************
*/                                                
                                                
    
public static string serPath;   //日志文件路径,事先指定                                                
    private string className;       //出错的类名                                                
                                                
    
public LogWrite(string className)                                                
    {                                            
        
this.className = className;                                                
                                                
        
//获取网站在服务器的物理位置                                                
        serPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;                                                
        
//                                        
        
// TODO: 在此处添加构造函数逻辑                                        
        
//                                        
    }                                                
                                                
    
#region "写日志"                                                
    
/// <summary>                                                
    
/// 写日志                                                
    
/// </summary>                                                
    
/// <param name="methodName">方法名称</param>                                                
    
/// <param name="errorInfo">错误信息</param>                                                
    public void WriteDebugLog(string methodName,string errorInfo)                                                
    {                                                
        
try                                                
        {                                                
            
///指定日志文件的目录                                                
            string fname = serPath + "\\LogInfo.txt";                                                
                                                
            
///定义文件信息对象                                                
            FileInfo finfo = new FileInfo(fname);                                                
                                                
            
///判断文件是否存在以及是否大于512K                                                
            if (finfo.Exists && finfo.Length > 100 * 1024 * 1024)                                                
            {                                                
                
///删除该文件                                                
                finfo.Delete();                                                
            }                                                
                                                
            
///创建只写文件流                                                
            using (FileStream fs = finfo.OpenWrite())                                                
            {                                                
                
///根据上面创建的文件流创建写数据流                                                
                StreamWriter w = new StreamWriter(fs);                                                
                                                
                
///设置写数据流的起始位置为文件流的末尾                                                
                w.BaseStream.Seek(0, SeekOrigin.End);                                                
                                                
                w.Write(
"<------------------------------------------------------------------------------------->\r\n");                                                
                                                
                
///写入“Debug Info: ”                                                
                w.Write("Debug时间  :");                                                
                                                
                
///写入当前系统时间                                                
                w.Write("{0} {1}\r\n", DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString());                                                
                                                
                
//写入出错的类名称                                                
                w.Write("Debug类名  :" + className + "\r\n");                                                
                                                
                
//写入出错的方法名称                                                
                w.Write("Debug方法名 :" + methodName + "\r\n");                                                
                                                
                
//写入错误信息                                                
                w.Write("Debug错误信息:" + errorInfo + "\r\n");                                                
                                                
                w.Write(
"<------------------------------------------------------------------------------------->\r\n");                                                
                                                
                
///清空缓冲区内容,并把缓冲区内容写入基础流                                                
                w.Flush();                                                
                                                
                
///关闭写数据流                                                
                w.Close();                                                
            }                                                
        }                                                
        
catch (Exception ex)                                                
        {                                                
            
string strEx = ex.ToString();                                                
        }                                                
    }                                                
    
#endregion                                                
}                                                

后台调用的方法:

在load事件之前:

DBOperation dbo = new DBOperation();                                
LogWrite lg;                        
//声明写日志的类                                
                                
private static string className = "设备维护修理记录[DevicesRunInfo.aspx]";     //当前页面                                

在catch语句快中:

lg = new LogWrite(className);                                
lg.WriteDebugLog(
"SaveDate()", ex.ToString());                                

 

免责声明:文章转载自《web中如何写log日志》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇windows 中如何定位恶意软件的藏身位置微信浏览器禁止页面下拉查看网址(不影响页面内部scroll)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Oracle RAC 集群环境下日志文件结构

Oracle RAC 集群环境下日志文件结构 在Oracle RAC环境中,对集群中的日志的定期检查是必不可少的。通过查看集群日志,可以早期定位集群环境中出现的问题,以便将问题消灭在萌芽状态。简单介绍一下有关Oracle集群环境中日志的结构,方便快速查找所需的日志文件。 1.Oracle集群日志藏匿之处 Orac 在Oracle RAC环境中,对集群中...

通过logstash过滤、分析日志数据

logstash是怎么工作的呢?   Logstash是一个开源的、服务端的数据处理pipeline(管道),它可以接收多个源的数据、然后对它们进行转换、最终将它们发送到指定类型的目的地。Logstash是通过插件机制实现各种功能的,读者可以在https://github.com/logstash-plugins 下载各种功能的插件,也可以自行编写插件。...

ORACLE 11G 闪回技术(闪回数据库、表、回收站)

ORACLE 11G 闪回技术(闪回数据库、表、回收站) weixin_45389319分类专栏: 数据库 文章标签: 数据库版权闪回数据库就是将数据库回退到过去的一个时间点或scn上,从而实现数据库的恢复,这种恢复不需要通过备份,所以更方便、更快速。该功能不基于撤销数据,而是基于闪回日志。 语法flashback 【standby】 database d...

windows下打开binlog

上篇我们介绍了binlog(参见mysql的binlog),配置文件用的是linux下的my.cnf,那么在windows下如何打开binlog呢?道理是相同的,配置文件是不一样的。在windows下我们需要配置my.ini,同名但不同后缀名。我们先来看我本地mysql的binlog开关是否打开: mysql> show variables like...

oracle之Flash Recovery Area全面介绍 (转载)

1. 设置闪回恢复区闪回恢复区主要通过3个初始化参数来设置和管理 db_recovery_file_dest:指定闪回恢复区的位置db_recovery_file_dest_size:指定闪回恢复区的可用空间大小 db_flashback_retention_target:指定数据库可以回退的时间,单位为分钟,默认1440分钟,也就是一天。当然,实际上可回...

pytest框架+allure2框架的基本使用(2019-09-03更新)

前提准备: pip安装allure-pytest、pytest和allure-python-commons包,安装过程如下图: pip install allure-pytest pip install pytest pip install allure-python-commons 安装成功如下: allure --version 2.12.1 py...