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();
    }
    }
    }
    }
    }

  • 相关阅读:
    linux 运维
    mariadb replication
    phpmyadmin
    Objective-C设计模式——单例Singleton(对象创建)
    收藏iOS学习资料
    axios拦截器
    vue单页面优化
    html设置http缓存代码
    js数组去重,排序的几种方法
    前端移动端问题
  • 原文地址:https://www.cnblogs.com/LiChen19951127/p/9967631.html
Copyright © 2011-2022 走看看