• C#日志记录类(待完善,考虑多线程)


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.IO;
    using System.Web.Util;
    using System.Threading;




    namespace CountApp.App_Code
    {
        public class SysLog
        {
            /// <summary>
            /// 创建文件夹
            /// </summary>
            public static void CreateLogFile()
            {
                string filePath = HttpContext.Current.Server.MapPath(@"~/temp/LogFolder/");
                if (!Directory.Exists(filePath))
                {
                    try
                    {
                        Directory.CreateDirectory(filePath);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
                if (!File.Exists(filePath + "LogFile.log"))
                {
                    try
                    {
                        FileStream file = File.Create(filePath + "LogFile.log");
                        file.Dispose();
                        file.Close();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }

            /// <summary>
            ///向日志文件中写入日志
            /// </summary>
            /// <param name="logText"></param>
            public static void LogWrite(string logText)
            {
                CreateLogFile();

                string filePath = HttpContext.Current.Server.MapPath(@"~/temp/LogFolder/LogFile.log");
                StreamWriter sw = new StreamWriter(filePath, true, System.Text.Encoding.UTF8);
                try
                {
                    sw.WriteLine("/********************************/");
                    sw.WriteLine("日期:" + System.DateTime.Now.ToString());
                    sw.WriteLine();
                    sw.WriteLine(logText);
                    sw.WriteLine("/********************************/");
                    sw.WriteLine();
                    sw.WriteLine();
                    sw.Flush();
                    sw.Dispose();
                    sw.Close();
                }
                catch (Exception ex)
                {
                    sw.Dispose();
                    sw.Close();
                }
            }

            public static void LogWrite(Exception ex)
            {
                CreateLogFile();
                string filePath = HttpContext.Current.Server.MapPath(@"~/temp/LogFolder/LogFile.log");
                StreamWriter sw = new StreamWriter(filePath, true, System.Text.Encoding.UTF8);
                try
                {
                    sw.WriteLine("/********************************/");
                    sw.WriteLine("日期:" + System.DateTime.Now.ToString());
                    sw.WriteLine();
                    sw.WriteLine("错误源:" + ex.Source);
                    sw.WriteLine("错误信息:" + ex.Message);
                    sw.WriteLine("/********************************/");
                    sw.WriteLine();
                    sw.WriteLine();
                    sw.Flush();
                    sw.Dispose();
                    sw.Close();
                }
                catch (Exception e)
                {
                    sw.Dispose();
                    sw.Close();
                }
            }
        }
    }
  • 相关阅读:
    ANR----以及如何定位是前端问题还是后台问题?
    一个App或者web端,你怎么展开测试
    创业性公司,你以什么方式来进行对真机的成本降低 ?
    判断查找IP的两种方法
    接口测试(四)总结及小技巧笔记
    接口测试 (三)功能分类及配置文件引入
    接口测试(二) 优化项目分层及cookies值带入
    接口测试模块完整版
    接口测试入门
    unittest框架(惨不忍睹低配版)
  • 原文地址:https://www.cnblogs.com/wangsx/p/2039076.html
Copyright © 2020-2023  润新知