zoukankan      html  css  js  c++  java
  • 写logo

     public static void WriteErrorLog(Exception ex)
            {
                string LOG_FOLDER = AppDomain.CurrentDomain.BaseDirectory + "Log";
                try
                {
                    //日志文件路径
                    filePath = LOG_FOLDER + "\\" + "ErrorLog" + ".txt";
                    if (!System.IO.Directory.Exists(LOG_FOLDER))
                    {
                        Directory.CreateDirectory(LOG_FOLDER);
                    }
                    if (!File.Exists(filePath))//如果文件不存在
                    {
                        File.Create(filePath);
                    }
                    StreamWriter sw = File.AppendText(filePath);
                    sw.WriteLine("-------------------------------------------------------------------------------------");
                    sw.WriteLine("Date:" + DateTime.Now.ToShortDateString() + " Time:" + DateTime.Now.ToShortTimeString());
                    sw.WriteLine(ex.Message);
                    sw.WriteLine(ex.StackTrace);
                    sw.WriteLine();
                    sw.Close();
                }
                catch (Exception)
                {
                    //throw;
                }
            } 

    上面的,经常出现"**文件正由另一进程使用,因此该进程无法访问该文件"!!!!!!!!!!!!!!!

    下面:

    publicstaticvoid WriteLog(string _msg)
        {
           
    string folder = System.Web.HttpContext.Current.Server.MapPath("~/log");
           
    if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);

           
    string filename = folder +"/"+ DateTime.Now.ToShortDateString() +".txt";
           
    if (File.Exists(filename))
            {
                FileStream fs
    =new FileStream(filename, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
                StreamWriter sr
    =new StreamWriter(fs);
                sr.WriteLine(DateTime.Now.ToString(
    "HH:mm:ss") +"\t"+ _msg);
                sr.Close();
                fs.Close();
            }
           
    else
            {
                FileStream fs
    =new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                StreamWriter sr
    =new StreamWriter(fs);
                sr.WriteLine(DateTime.Now.ToString(
    "HH:mm:ss") +"\t"+ _msg);
                sr.Close();
                fs.Close();
            }      
        }

  • 相关阅读:
    PowerShell2.0之Windows排错(六)检查网络故障
    确保数据安全是云计算取信于用户的关键
    企业发展如何借助“云的力量”
    PowerShell2.0之维护网络(三)设置网络适配器
    Feign最佳实践
    Nacos注册中心原理
    GateWay网关快速入门
    Nacos集群搭建
    Feign快速入门
    Feign的性能优化
  • 原文地址:https://www.cnblogs.com/gerryge/p/2567711.html
Copyright © 2011-2022 走看看