zoukankan      html  css  js  c++  java
  • C#日志写入

    public class Logs
        {
            /// <summary>
            /// 写日志,指定日志文件
            /// </summary>
            /// <param name="File"></param>
            /// <param name="Msg"></param>
            public static void Info(string Msg)
            {
                try
                {
                    string fileName = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log\Info\info-" + DateTime.Now.ToString("yyyyMMdd") + ".lg");
                    string path = Path.GetDirectoryName(fileName);
                    if (!System.IO.Directory.Exists(path))
                    {
                        System.IO.Directory.CreateDirectory(path);
                        System.IO.File.CreateText(fileName).Dispose();
                    }
                    else if (!System.IO.File.Exists(fileName))
                    {
                        System.IO.File.CreateText(fileName).Dispose();
                    }
                    using (TextWriter writer2 = System.IO.File.AppendText(fileName))
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.AppendLine("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
                        sb.AppendLine("消息记录时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                        sb.AppendLine(Msg);
                        sb.AppendLine(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                        sb.AppendLine("");
                        writer2.WriteLine(sb.ToString());
                    }
                }
                catch (Exception ex)
                {
    
                }
            }
    
            public static void Error(string Title, Exception exception)
            {
                try
                {
                    string fileName = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log\Error\err-" + DateTime.Now.ToString("yyyyMMdd") + ".lg");
                    string path = Path.GetDirectoryName(fileName);
                    if (!System.IO.Directory.Exists(path))
                    {
                        System.IO.Directory.CreateDirectory(path);
                        System.IO.File.CreateText(fileName).Dispose();
                    }
                    else if (!System.IO.File.Exists(fileName))
                    {
                        System.IO.File.CreateText(fileName).Dispose();
                    }
                    using (TextWriter writer2 = System.IO.File.AppendText(fileName))
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.AppendLine("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
                        sb.AppendLine("程序发生异常:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                        sb.AppendLine("异常标题:" + Title);
                        GetExceptionMsg(exception, sb, "");
                        sb.AppendLine(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                        sb.AppendLine("");
                        writer2.WriteLine(sb.ToString());
                    }
                }
                catch (Exception ex)
                {
    
                }
            }
    
            static void GetExceptionMsg(Exception ex, StringBuilder sb, string Prefix = "")
            {
                sb.AppendLine(Prefix + "【异常类型】:" + ex.GetType().Name);
                sb.AppendLine(Prefix + "【异常信息】:" + ex.Message);
                sb.AppendLine(Prefix + "【堆栈调用】:" + ex.StackTrace);
                sb.AppendLine(Prefix + "【异常方法】:" + ex.TargetSite);
    
                if (ex.InnerException != null)
                    GetExceptionMsg(ex.InnerException, sb, Prefix + "	");
            }
        }
    

      

    慎于行,敏于思!GGGGGG
  • 相关阅读:
    基于xmpp openfire smack开发之Android客户端开发[3]
    简单工厂,抽象工厂,工厂模式三者的对照
    【独具慧眼 推荐有礼】找出您心中的技术大牛 活动開始啦!
    ZOJ 3201
    具体解释站点沙盒期的原因表现与解决的方法
    工作,究竟意味着什么
    MVC之查询demo
    学习开淘宝网店
    快排的两种写法
    QTableWidget具体解释(样式、右键菜单、表头塌陷、多选等) (非代理)
  • 原文地址:https://www.cnblogs.com/GarsonZhang/p/5420652.html
Copyright © 2011-2022 走看看