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();
            }      
        }

  • 相关阅读:
    就为了一个原子操作,其他CPU核心罢工了
    浅谈JVM和垃圾回收
    简单了解一下K8S,并搭建自己的集群
    WebAssembly完全入门——了解wasm的前世今身
    【简单了解系列】从基础的使用来深挖HashMap
    【俗话说】换个角度理解TCP的三次握手和四次挥手
    两分钟让你明白Go中如何继承
    游戏服务器和Web服务器的区别
    Go中使用seed得到相同随机数的问题
    从web到游戏,走出舒适区
  • 原文地址:https://www.cnblogs.com/gerryge/p/2567711.html
Copyright © 2011-2022 走看看