• 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
  • 相关阅读:
    如何设置ASP.NET页面的运行超时时间
    日志文件清理工具V1.1
    【原创】日志文件清理工具V1.0
    【分享】国外后台界面HTML源码 [免费]
    【分享】仿东软OA协同办公服务管理源码
    年底发福利了——分享一下我的.NET软件开发资源
    由12306动态验证码想到的ASP.NET实现动态GIF验证码(附源码)
    【分享】元旦送礼,商业源码免费赠送!
    给大家分享一个jQuery TAB插件演示
    【分享】双12了,也没啥可送大家的,就送大家点商业源码吧!
  • 原文地址:https://www.cnblogs.com/GarsonZhang/p/5420652.html
Copyright © 2020-2023  润新知