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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/417728.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

iOS10 UI教程管理层次结构

iOS10 UI教程管理层次结构 iOS10 UI教程管理层次结构,在一个应用程序中,如果存在多个层次结构,就需要对这些层次结构进行管理。在UIView类中提供了可以用来管理层次结构的方法,让开发者可以添加、移动、删除来自层次结构中的元素。…

appium()-The event firing

原文地址:https://github.com/appium/java-client/blob/master/docs/The-event_firing.md since 4.1.0 The purpose This feature allows end user to organize the event logging on the client side. Also this feature may be useful in a binding with standard…

c# oracle datasource,C# 连接Oracle 数据库 示例源码下载

【实例简介】C# 实现 Oracle 数据库的 增删改查 操作【实例截图】【核心代码】using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using S…

前端学习(2169):vue-router安装和配置方式

main.js import VueRouter from vue-router import Vue from vue//安装插件 const routers new VueRouter({//配置之间的关系routes })export default router index.js import VueRouter from vue-router import Vue from vue//安装插件 const routers new VueRouter({//配…

如何在intellj Idea中给新建的项目添加jar包?

1. 假如我加入joda.jar 2. 找到发布的你想要的jar包,下载! 3. 解压刚下载的jar包,复制 4. 在intellj idea中新建一个java项目,然后创建一个专门用于放jar的lib文件夹, 然后添加ctrlv 黏贴刚复制的jar包, 然…

js声明php变量,vue.js怎样声明变量

vue.js声明变量的方法:1、使用let定义,let是块级作用域,函数内部使用let定义后,对函数外部无影响;2、使用var定义,var定义的变量可以修改;3、使用const定义,const定义的变量不可以修…

前端学习(2173):动态路由的跳转

app.vue <template><div id"app"><router-link to"/home">首页</router-link><router-link to"/about">关于</router-link><router-link v-bind:to"/user/userId">用户</router-link&g…

前端测试框架 jasmine 的使用

最近的项目在使用AngulaJs,对JS代码的测试问题就摆在了面前。通过对比我们选择了 Karma jasmine ,使用 Jasmine做单元测试 &#xff0c;Karma 自动化完成&#xff0c;当然了如果使用 Karma jasmine 前提是必须安装 Nodejs。 安装好 Nodejs &#xff0c;使用 npm 安装好必要…

(HDU)1019 --Least Common Multiple(最小公倍数)

描述 一组正整数的最小公倍数&#xff08;LCM&#xff09;是可以被集合中所有数字整除的最小正整数。 例如&#xff0c;5,7和15的LCM为105。输入 输入将包含多个问题实例。 输入的第一行将包含指明问题实例数量的单个整数。 每个实例将由形式为m n1 n2 n3 ... nm的单行组成&…

如何将exe文件在linux下执行,如何在Linux系统下查找可执行文件

可执行文件是指可移植可执行的文件&#xff0c;用于程序的执行&#xff0c;那么Linux下要如何查找可执行文件呢&#xff1f;下面小编就给大家介绍下Linux中查找可执行文件的方法&#xff0c;一起来了解下吧。linux下查找可执行文件ls -F|grep “*”这样就可以了&#xff01;ls …

前端学习(2176):vue-router的路由的嵌套使用

app.vue <template><div id"app"><router-link to"/home">首页</router-link><router-link to"/about">关于</router-link><router-link v-bind:to"/user/userId">用户</router-link&g…

前端学习(2177):vue-router得参数传递

app.vue <template><div id"app"><router-link to"/home">首页</router-link><router-link to"/about">关于</router-link><router-link v-bind:to"/user/userId">用户</router-link&g…

前端学习(2178):vue-router得参数传递二

app.vue <template><div id"app"><router-link to"/home">首页</router-link><router-link to"/about">关于</router-link><button click"userClick">用户</button><button clic…

linux系统登陆问题,Linux之登陆问题

今天早上在使用Linux的时候进入终端输入startx&#xff0c;然后退出图形界面&#xff0c;进入了命令模式&#xff0c;可能是Ubuntu 14.04的问题&#xff0c;不知怎么就没有响应&#xff0c;我就强行重启了一下操作系统&#xff0c;然后进去发现在使用管理员账号登录时一直是重复…