asp.net记录错误日志的方法
生活随笔
收集整理的這篇文章主要介紹了
asp.net记录错误日志的方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1、說明
在調試發(fā)布后的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的文件夾,如果沒有該文件夾會自動創(chuàng)建
? ? ??
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/?
本文版權歸【冷戰(zhàn)】和博客園所有,歡迎轉載收藏,未經(jīng)作者同意須保留此段聲明,否則保留追究法律責任的權利。
轉載于:https://www.cnblogs.com/lengzhan/p/6100419.html
總結
以上是生活随笔為你收集整理的asp.net记录错误日志的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cmd-ssh使用说明
- 下一篇: 使用ildasm获取源代码_有什么比IL