asp.net记录错误日志的方法

1、说明

  在调试发布后的asp.net项目时有可能会遇到意想不到的错误,而未能及时的显示。这就需要记录日志来跟踪错误信息,所以写了个简单的记录信息的方法,记录简单的文本信息也可以使用。此方法是以生成文本文件的方式记录的,下面贴出代码

2、代码

需要引用 using System.IO;

byte[] myByte = System.Text.Encoding.UTF8.GetBytes("这里是你想要的记录的文本信息");
string strPath = Server.MapPath("~") + "\\Log\\";
if (!Directory.Exists(strPath))
{
    Directory.CreateDirectory(strPath);
}
string strPathLog = strPath + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt";
using (FileStream fsWrite = new FileStream(strPathLog, FileMode.Append))
{
    fsWrite.Write(myByte, 0, myByte.Length);
};

  这里会在项目的根目录下生成一个Log的文件夹,如果没有该文件夹会自动创建

      

3、示例

try {
    int i = Convert.ToInt32("");
}
catch (Exception ex) {
    byte[] myByte = System.Text.Encoding.UTF8.GetBytes(ex.ToString());
    string strPath = Server.MapPath("~") + "\\ErrorLog\\";
    if (!Directory.Exists(strPath))
    {
        Directory.CreateDirectory(strPath);
    }
    string strPathLog = strPath + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt";
    using (FileStream fsWrite = new FileStream(strPathLog, FileMode.Append))
    {
        fsWrite.Write(myByte, 0, myByte.Length);
    };
}

  

 

作者:小路 QQ:2490024434 
出处:http://www.cnblogs.com/lengzhan/ 
本文版权归【冷战】和博客园所有,欢迎转载收藏,未经作者同意须保留此段声明,否则保留追究法律责任的权利。

转载于:https://www.cnblogs.com/lengzhan/p/6100419.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值