前言:由于现在日志非常重要,但是在哪里打写日志比较好呢,我选择的是在global中,把错误代码网上抛,而不是在底层写大量的try catch然后在catch中来写日志,每个catch中的写日志这样就会避免了很多重复代码。当然这是目前我们采取的一个方法,大家可以提出更好地方法来管理日志,下面我开始写代码

第一步:尽量抛弃项目中try catch。看下代码

private void ExceptionTestOne()
{
int a = ;
int b = ;
int c = a/b;
}

上面代码会抛一个异常

第二步:如果项目中必须用try catch怎么办,因为有时候连接wcf的时候如果出现异常时候我们需要关闭连接避免堵塞,那么我们就采取抛异常的方法

private void ExceptionTestTwo()
{
try
{
List<Simple> simples = null;
simples.Add(new Simple() { Name = "发生异常" });
}
catch (Exception ex)
{
throw new Exception("",ex);
}
}

第三步:我们新建一个global.asax 在Application_Error中把异常信息加入队列中 如下

 //创建一个静态的队列记录把异常都放在队列中
private static Queue<Exception> queue = new Queue<Exception>(); protected void Application_Error(object sender, EventArgs e)
{
Exception exp = Server.GetLastError();
if (exp != null) {
queue.Enqueue(exp);
} Server.ClearError();
}

第四步:异常信息加入日志(这里为了简单就写入txt文本中了)

protected void Application_Start(object sender, EventArgs e) {
ThreadPool.QueueUserWorkItem(a => {
while (true) {
try {
if (queue.Count > ) {
Exception ex = queue.Dequeue();
WriteLog(ex);
}
else {
System.Threading.Thread.Sleep(TimeSpan.FromSeconds());
}
}
catch (Exception ex) {
                    WriteLog(ex);

} } }); }
    private void WriteLog(Exception ex)
{
if (!File.Exists(@"E:\C#系列\ExceptionDealWith\ExceptionDealWith\Log.txt")) {
FileStream fs1 = new FileStream(@"E:\C#系列\ExceptionDealWith\ExceptionDealWith\Log.txt", FileMode.Create, FileAccess.Write);//创建写入文件
StreamWriter sw = new StreamWriter(fs1);
sw.WriteLine(ex.Message);//开始写入值
sw.Close();
fs1.Close();
}
else
{
StreamWriter sr = File.AppendText(@"E:\C#系列\ExceptionDealWith\ExceptionDealWith\Log.txt");
sr.WriteLine(ex.Message);
sr.Close();
}
}

当然接入错误信息你可以多定义几个比喻ip地址,堆栈信息错误等基本就是这么多了。这是基本思路。有日志了我就就可以根据时间,ip等进行查询日志日志信息

下载

global中捕获异常的更多相关文章

  1. Global中的事件执行顺序

    The Global.asax file, sometimes called the ASP.NET application file, provides a way to respond to ap ...

  2. tp5中捕获异常的配置

    首选在配置文件中加入配置如下 // 异常处理handle类 留空使用 \think\exception\Handle    'exception_handle'       => '\\app\ ...

  3. asp.net4.0在Global中的Application_Start 中直接或间接使用 HttpUtility.UrlEncode等出现异常Response is not available in this context的解决方法

    HttpUtility.HtmlEncode HttpUtility.HtmlDecode HttpUtility.UrlEncode HttpUtility.UrlDecode 也会出现此异常. 这 ...

  4. global中拦截404错误的实现方法

    1. void Application_Error(object sender, EventArgs e) { if(Context != null) { HttpContext ctx = Http ...

  5. node 进阶 | 通过node中如何捕获异常阐述express的特点

    node如何捕获异常 node基于js的单线程,有了非阻塞异步回调的概念,但是在处理多个并发连接时,并发环境要求高,最重要的是单线程,单核CPU,一个进程crash则web服务都crash,但是为什么 ...

  6. asp.net中当服务器出错时显示指定的错误页面

    http://blog.csdn.net/helloxiaoyu/article/details/2943537 此篇文章描述了当异常再ASP.NET中发生时怎样使用C#.NET代码去拦截和相应异常. ...

  7. ASP.NET中处理异常的几种方式

    1.程序中使用try catch 对于预知会发生异常的代码段使用try catch主动捕获异常,适用于提示给用户或跳转到错误页面,或者通过其它方式处理异常(日志.通知等). int i = 10; i ...

  8. WebAPI中无法获取Session对象的解决办法

    在MVC的WebApi中默认是没有开启Session会话支持的.需要在Global中重写Init方法来指定会话需要支持的类型 public override void Init() { PostAut ...

  9. iOS 中block中使用了外部变量的分析

    例子1: ; void (^blk)(void) = ^(){ printf("in block %d[%p]\n", val, &val); //in block 10[ ...

随机推荐

  1. flutter 配置环境

    1. 下载java SDK https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html c ...

  2. [Python 多线程] multiprocessing、多进程、工作进程池 (十四)

    由于Python的GIL限制,多线程未必是CPU密集型程序的好的选择. 多进程可以完全独立的进程环境中运行程序,可以充分地利用多处理器. 但是进程本身的隔离性带来的数据不共享也是一个问题.而且线程比进 ...

  3. 递归根据父ID 找所有子类ID

    function getinfo($pid){ $str = ''; $row = M('user')->where(array('pid'=>$pid))->select(); i ...

  4. 谷歌浏览器linux,windows下载

    https://www.chromedownloads.net/ 提取码自己行提取rpm安装包

  5. 如何利用java程序实现加密所需的公钥、密钥、数字证书

    本篇的主要目的在于实现pdf的数字签名问题,只是作为我学习知识的总结. 1.数字签名算法的概述 本部分主要参考于:https://blog.csdn.net/lovelichao12/article/ ...

  6. HDU 3938 Portal (离线并查集,此题思路很强!!!,得到所谓的距离很巧妙)

    Portal Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  7. unordered_map 遇到 vector subscript out of range 的错误提示

    错误类型 当调用unordered_map的函数的时候,会出现如下问题: 使用linux运行则会提示 float exeption(core dump) 原因 遇到vector subscript o ...

  8. DPDK运行出现EAL Error reading from file descriptor 23 Input output error

    原因 dpdk不支持该网卡导致,需要修改一行代码,跳过dpdk pci 检查. 解决方法 修改lib/librte_eal/linuxapp/igb_uio/igb_uio.c 将文件中该行修改 pc ...

  9. 并发编程(三)------并发类容器Copy-On-Write容器

    Copy-On-Write简称COW,是一种用于程序设计中的优化策略.JDK里的COW容器有两种: CopyOnWriteArrayList CopyOnWriteArraySet CopyOnWri ...

  10. WebClient 下载图片(文件)

    public static string SaveImage(string url, string newfilename) { WebClient mywebclient = new WebClie ...