zoukankan      html  css  js  c++  java
  • 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();
    }
    }
    }
    }
    }

  • 相关阅读:
    快速排序
    冒泡排序
    选择排序
    合并排序
    插入排序
    跟我一起阅读Java源代码之HashMap(三)
    跟我一起阅读Java源代码之HashMap(二)
    跟我一起阅读Java源代码之HashMap(一)
    Apache2.2 + tomcat7 服务器集群配置
    Spring+Hibernate实现动态SessionFactory切换(改进版)
  • 原文地址:https://www.cnblogs.com/LiChen19951127/p/9967631.html
Copyright © 2011-2022 走看看