公布一个简单的日志记录方法

 

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值