• C# 后台报错输出到日志


    1.C# 方法

    /// <summary>
    ///  异常处理
    /// </summary>
    /// <returns></returns>
    public void  GetLogInfo()
    {
        string msg = string.Empty;
    
        try
        {
           string str="这是程序代码";
        }
        catch (Exception ex)
        {
            msg = ex.Message;
            isSavedSuccessfully = false;
            WoExcepitonLog wl = new WoExcepitonLog();
            wl.WriteLog(ex, "WO_Exception.txt");
            wl.CreateLog(ex, (this.SessionExt().Get<SessionUser>() as SessionUser).UserId, WOExceptionType.SaveException, "");
            throw new TeldWOException(WOExceptionType.SaveException, ex);
        }
    }

    2.输出日志方法

    public class ExcepitonLog
    {
        private static object m_Lock = new object();
        string dic = @"C:Logs";
        string RealTimeData = "WO_RealTimeData.txt";//运行数据
        string RealTimeException = "WO_Exception.txt";//异常
        public void WriteLog(Exception ce, string fileName)
        {
            if (!Directory.Exists(dic))//判断是否存在
            {
                Directory.CreateDirectory(dic);//创建新路径
            }
            lock (m_Lock)
            {
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(dic + "\" + fileName, true))
                {
                    file.WriteLine("------------------------------" + DateTime.Now.ToString() + "------------------------------");
                    file.WriteLine(ce.StackTrace);
                    file.WriteLine(ce.Source);
                    file.WriteLine(ce.TargetSite);
                    file.WriteLine(ce.Message);
                }
            }
        }
    }
    

      后续完善

  • 相关阅读:
    SpringMVC-初学习
    Mybatis-随笔
    Mybatis-逆向工程generator
    Mybatis-动态sql和模糊查询
    Mybatis-resultMap的一些用法(映射,关联查询)
    Mybatis基本的CRUD
    Spring框架(一)
    restful风格以及异常处理。
    SpringMVC后台校验
    Spring添加文件
  • 原文地址:https://www.cnblogs.com/fengduandeai/p/8781511.html
Copyright © 2020-2023  润新知