C# 异常日志记录
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
namespace HanXingExam.Common
{
public class ErrorLog
{
public static void WriteLog(Exception ex)
{
string errorTime = "异常时间:" + DateTime.Now.ToString();
string errorAddress = "异常地址:" + HttpContext.Current.Request.Url.ToString();
string errorInfo = "异常信息:" + ex.Message;
string errorSource = "错误源:" + ex.Source;
string errorType = "运行类型:" + ex.GetType();
string errorFunction = "异常函数:" + ex.TargetSite;
string errorTrace = "堆栈信息:" + ex.StackTrace;
HttpContext.Current.Server.ClearError();
System.IO.StreamWriter writer = null;
try
{
//写入日志
string path = string.Empty;
path = HttpContext.Current.Server.MapPath("~/ErrorLogs/");
//不存在则创建错误日志文件夹
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path += string.Format(@"\{0}.txt", DateTime.Now.ToString("yyyy-MM-dd"));
writer = !System.IO.File.Exists(path) ? System.IO.File.CreateText(path) : System.IO.File.AppendText(path); //判断文件是否存在,如果不存在则创建,存在则添加
writer.WriteLine("用户IP:" + HttpContext.Current.Request.UserHostAddress);
writer.WriteLine(errorTime);
writer.WriteLine(errorAddress);
writer.WriteLine(errorInfo);
writer.WriteLine(errorSource);
writer.WriteLine(errorType);
writer.WriteLine(errorFunction);
writer.WriteLine(errorTrace);
writer.WriteLine("********************************************************************************************");
}
finally
{
if (writer != null)
{
writer.Close();
}
}
}
}
}
C# 异常日志记录的更多相关文章
- Log4Net异常日志记录在asp.net mvc3.0的应用
前言 log4net是.Net下一个非常优秀的开源日志记录组件.log4net记录日志的功能非常强大.它可以将日志分不同的等级,以不同的格式,输出到不同的媒介.本文主要是简单的介绍如何在Visual ...
- Log4Net异常日志记录在asp.net mvc3.0的应用(转载)
这篇博客写的很好:http://www.cnblogs.com/qianlifeng/archive/2011/04/22/2024856.html 前言 log4net是.Net下一个非常优秀的开源 ...
- ASP.NET全局错误处理和异常日志记录以及IIS配置自定义错误页面
应用场景和使用目的 很多时候,我们在访问页面的时候,由于程序异常.系统崩溃会导致出现黄页.在通常的情况下,黄页对于我们来说,帮助是极大的,因为它可以帮助我们知道问题根源,甚至是哪一行代码出现了错误.但 ...
- IOS异常日志记录与展现功能
在平常的APP开发过程中经常碰到程序遇到异常闪退的问题,通过日志可以把相关的详细错误信息进行记录,本实例要记录不管在哪个页面出错都要进行记录,这边使用到的日志记录插件CocoaLumberjack,以 ...
- 从壹开始前后端分离 [.netCore 不定期更新 ] 三十五║ 完美实现全局异常日志记录
缘起 哈喽我是不定期更新的日常,昨天群里小伙伴问到了记录日志,当然,以前我也挖过这个坑,后来一直没有来得及填上,也想着 swagger 一直又有错误信息展示的功能,就迟迟没有添加这个功能,不过昨天夜里 ...
- OneAPM大讲堂 | Java 异常日志记录最佳实践
[编者按]本文作者是 Casey Dunham.Casey 是一位具有 10 多年经验的专业软件开发人员,以其独特的方式应对应用安全问题而闻名.本文系国内 ITOM 管理平台 OneAPM 工程师编译 ...
- 转:使用log4net完成程序异常日志记录(使用SQLite数据库记录和普通文本记录)
http://www.cnblogs.com/kyo-yo/archive/2010/06/11/use-log4net-to-log-exception.html 在前端时间开发的时候由于需要将异常 ...
- ASP.NET Web API 异常日志记录
如果在 ASP.NET MVC 应用程序中记录异常信息,我们只需要在 Global.asax 的 Application_Error 中添加代码就可以了,比如: public class MvcApp ...
- python中用修饰器进行异常日志记录
当脚本中需要进行的的相同的异常操作很多的时候,可以用修饰器来简化代码.比如我需要记录抛出的异常: 在log_exception.py文件中, import functools import loggi ...
随机推荐
- 使用copy函数输出容器中的内容
container<type> c; 输出语句为:copy(c.begin(), c.end(), ostream_iterator<type>(cout, " &q ...
- Yum -y update 报错
问题描述: 操作系统:CentOS 6.5 今天服务器上执行 yum -y update 命令时,提示: Running rpm_check_debug ERROR with rpm_check_de ...
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 18—Photo OCR 应用实例:图片文字识别
Lecture 18—Photo OCR 应用实例:图片文字识别 18.1 问题描述和流程图 Problem Description and Pipeline 图像文字识别需要如下步骤: 1.文字侦测 ...
- 浅谈JobExecutionContext&JobDataMap
- kubernetes ServiceAccount
api server 启动参数增加 –admission_control=…,ServiceAccount,…
- Using Mono DLLs in a Unity Project
[Using Mono DLLs in a Unity Project] The path to the Unity DLLs will typically be: 一个生成dll的例子如下: mcs ...
- 字符图元 & 显示列表
[字符图元] 1.typeface(字样),即设计风格,如Courier等. 2.font(字体),如10磅Courier斜体. 3.monspace即为等宽字体,proportional为非等宽字体 ...
- Scala基础:面向对象之trait
trait类似于java中的interface,但是有所不同 Scala中的trait是一种特殊的概念: 首先先将trait作为接口使用,此时的trait就与Java中的接口 (interface)非 ...
- windows系统mysql-5.7.19官方绿色版zip包安装教程
环境: 系统环境 Windows 10 64位 mysql版本 5.7.19 一.万变不离的下载 下载页面:https://dev.mysql.com/downloads/mysql/ 点击 Down ...
- 让tomcat自动定位到项目
在servelt.xml中添加粗体部分. <Host name="localhost" appBase="webapps" unpackWARs=&quo ...