没有复杂的算法,也没有打算用log4net之类的东东。只要这个,就可以在目录的文件中,看到日志信息

一句话:简单实用。

 

 

 public static void Log(string message)
        {
            if (message != "")
            {
                Random randObj = new Random(DateTime.Now.Millisecond);
                int file = randObj.Next() + 1;
                string filename = DateTime.Now.ToString("yyyyMMdd") + ".txt";
                try
                {
                    FileInfo fi = new FileInfo(HttpContext.Current.Server.MapPath("~//Log//" + filename));
                    if (!fi.Exists)
                    {
                        using (StreamWriter sw = fi.CreateText())
                        {
                            sw.WriteLine(DateTime.Now + "   \n" + message + System.Environment.NewLine);
                            sw.Close();
                        }
                    }
                    else
                    {
                        using (StreamWriter sw = fi.AppendText())
                        {
                            sw.WriteLine(DateTime.Now + "   \n" + message + System.Environment.NewLine);
                            sw.Close();
                        }
                    }
                }
                catch
                {
                }
            }
        }

把 HttpContext.Current.Server 用System.AppDomain.Current 可以用在CS应用程序中。

 

相关文章:

  • 2022-01-26
  • 2022-12-23
  • 2021-06-11
  • 2022-12-23
  • 2022-12-23
  • 2021-10-20
  • 2022-02-25
  • 2022-12-23
猜你喜欢
  • 2021-11-09
  • 2021-11-25
  • 2022-12-23
  • 2021-08-20
  • 2021-06-29
  • 2021-06-24
  • 2023-02-08
相关资源
相似解决方案