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

  • 相关阅读:
    django 基础1
    django 权限管理
    django 统计表
    关于RF中类似于异常(TRY语句)情况的处理
    关于提BUG的一点思考以及工作中总结的规范
    关于爬虫的学习
    关于RF中元素定位问题
    RobotFrameWork(五)控制流之if语句——Run Keyword If
    最完整的自动化测试流程
    关于python语言学习心得
  • 原文地址:https://www.cnblogs.com/LiChen19951127/p/9967631.html
Copyright © 2011-2022 走看看