zoukankan      html  css  js  c++  java
  • 公布一个简单的日志记录方法

     没有复杂的算法,也没有打算用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应用程序中。

  • 相关阅读:
    Web标准:五、超链接伪类
    Spring Security(16)——基于表达式的权限控制
    Spring Security(15)——权限鉴定结构
    Spring Security(14)——权限鉴定基础
    Spring Security(13)——session管理
    Spring Security(12)——Remember-Me功能
    Spring Security(11)——匿名认证
    Spring Security(10)——退出登录logout
    Spring Security(09)——Filter
    Spring Security(08)——intercept-url配置
  • 原文地址:https://www.cnblogs.com/JamesLi2015/p/1318180.html
Copyright © 2011-2022 走看看