• 输出log文件


            /// <summary>
            /// 写入日志文件
            /// </summary>
            /// <param name="input"></param>
            public static void WriteLogFile(string input)
            {
    
                string getPath = System.Web.HttpContext.Current.Server.MapPath("~/logs/");
                if (!Directory.Exists(getPath))
                    Directory.CreateDirectory(getPath);
                ///指定日志文件的目录
                string fname = getPath +
                DateTime.Now.Year + '-' +
                DateTime.Now.Month + '-' +
                DateTime.Now.Day + "_LogFile" + ".txt";
    
                if (!File.Exists(fname))
                {
                    //不存在文件
                    File.Create(fname).Dispose();//创建该文件
                }
    
                /**/
                ///判断文件是否存在以及是否大于2K
                /* if (finfo.Length > 1024 * 1024 * 10)
                {
                /**/
                //文件超过10MB则重命名
                /* File.Move(fname, Directory.GetCurrentDirectory() + DateTime.Now.TimeOfDay + "\LogFile.txt");
                //删除该文件
                //finfo.Delete();
                }*/
    
                using (StreamWriter log = new StreamWriter(fname, true))
                {
                    //FileStream fs = new FileStream(url, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);FileMode.Append
    
                    ///设置写数据流的起始位置为文件流的末尾
                    log.BaseStream.Seek(0, SeekOrigin.End);
    
                    ///写入“Log Entry : ”
                    log.Write("
    
    Log Entry : ");
    
                    ///写入当前系统时间并换行
                    log.Write("{0} {1} 
    
    ", DateTime.Now.ToLongTimeString(),
                    DateTime.Now.ToLongDateString());
    
                    ///写入日志内容并换行
                    log.Write(input + "
    
    ");
    
                    //清空缓冲区
                    log.Flush();
                    //关闭流
                    log.Close();
                }
            }
    
    
    
            /// <summary>
            /// 将异常打印到LOG文件
            /// </summary>
            /// <param name="ex">异常</param>
            /// <param name="LogAddress">日志文件地址</param>
            public static void WriteLogTxt(Exception ex, string LogAddress = "")
            {
                //如果日志文件为空,则默认在Debug目录下新建 YYYY-mm-dd_Log.log文件
                if (LogAddress == "")
                {
                    ///指定日志文件的目录
                    LogAddress = "D:\" +
                    DateTime.Now.Year + '-' +
                    DateTime.Now.Month + '-' +
                    DateTime.Now.Day + "_Log.log";
                }
    
                FileInfo finfo = new FileInfo(LogAddress);
    
                if (!finfo.Exists)
                {
                    FileStream fs;
                    fs = File.Create(LogAddress);
                    fs.Close();
                    finfo = new FileInfo(LogAddress);
                }
    
                //把异常信息输出到文件
                StreamWriter sw = new StreamWriter(LogAddress, true);
                sw.WriteLine("当前时间:" + DateTime.Now.ToString());
                sw.WriteLine("异常信息:" + ex.Message);
                sw.WriteLine("异常对象:" + ex.Source);
                sw.WriteLine("调用堆栈:
    " + ex.StackTrace.Trim());
                sw.WriteLine("触发方法:" + ex.TargetSite);
                sw.WriteLine();
                sw.Close();
            }
  • 相关阅读:
    Button与Submit调用前台与后台代码的方法
    ASP.NET前台代码绑定后台变量方法总结
    点击后禁用提交按钮
    SilverLight3测试(1): Message: AG_E_NETWORK_ERROR
    SilverLight 问题解决
    XmlSerializers.dll.deploy 未成功
    命令模式泛型?问题?
    SilverLight 收藏(2009年3月27日)
    在Web安装工程中为虚拟目录指定不同的本地路径
    Silverlight 3 Sample Application: Slidentity
  • 原文地址:https://www.cnblogs.com/s666/p/14280607.html
Copyright © 2020-2023  润新知