我编写了一个记录访问我网站的信息的日志,我会把1000条前先放在Application缓存中,当超过1000我就会写进日志,这样一开始好好的,但是会在某个时候突然日志很大,发现日志内有很大部分是重复的数据,那肯定是缓存没有获取的新的信息,而且也没有清空原来的数据,造成不断把缓存中的数据写入日志文件,这样也让我丢失了那些时段的数据,只有重启IIS才恢复,我想问下为什么会出现这样的情况,缓存不变了是什么原因?
解决方案 »
- “/”应用程序中的服务器错误。急急~~~~~~~~~~~~~~~~~~~~~~~
- vs2008为什么有时候点击服务器端按钮,却自动在aspx页面里面加了点击过程的代码,而不是CS?
- 我的Visual Studio 2005一启动调试占用系统盘空间?
- 怎样才能实现把数据库中整型日期型,浮点型,在应用层转化为String类型啊
- 获取用户真是IP的类的写法
- 当前页面是utf-8编码是,保存进数据库时是否需要对数据进行编码?
- 请问asp.net页面中如何嵌入active form?
- 谁能带我学asp.net c# ???定期给高分
- 菜鸟问,在DataGrid中如何操作“连接列hyperlinkcolumn”?
- 怎样让母版页撑满全屏幕
- 求助~~程序出问题不知怎么解决
- asp.net2.0 textbox问题。想实现给textbox输入文本时,自动将内容显示在lable上。
if(Convert.ToInt32(HttpContext.Current.Application["ComLogNum"]) < 1000){
HttpContext.Current.Application["ComLogRecord"] = Convert.ToString(HttpContext.Current.Application["ComLogRecord"]) + log;
HttpContext.Current.Application["ComLogNum"] = Convert.ToInt32(HttpContext.Current.Application["ComLogNum"]) + 1;
}
else
{
// 判断日志文件是否存在
StreamWriter g = File.AppendText(strPath);
// 一行一行写如日志文件
g.WriteLine(Convert.ToString(HttpContext.Current.Application["ComLogRecord"]));
g.Close();
HttpContext.Current.Application["ComLogRecord"] = log;
HttpContext.Current.Application["ComLogNum"] = 1;
}
public class LogInfo
{
private string content;
private DateTime time;
private int level; public string Content
{
get{return content;}
set{content=value;}
}
//其它属性自己加 public LogInfo(){}
} public interface ILog
{
//当前缓冲日志条数
int CurrentCount{get;}
//添加日志信息
void AddLog(LogInfo info);
//将缓冲保存至存储介质
bool Save();
}
实现此接口 public class LogFactory
{
private ILog log=null;//=new xxx实现了ILog接口的类
private LogFactory(){} public static ILog getLog()
{
return log;
}
}
public interface ILog
{
int Max{get;set;}
//当前缓冲日志条数
int CurrentCount{get;}
//添加日志信息
void AddLog(LogInfo info);
//将缓冲保存至存储介质
bool Save();
}
LogFactory修改下
public class LogFactory
{
// private static ILog log=null;//=new xxx实现了ILog接口的类
private static ILog log=new Log();
private LogFactory(){} public static ILog getLog()
{
return log;
}
}下面实现类(部分自己实现一下)
public class Log:ILog
{
private int max=1000;
private IList list=new ArrayList();
public Log(){} #region ILog 成员 public int Max
{
get{return max;}
set{max=value;}
} public int CurrentCount
{
get{return list.Count;}
} public void AddLog(LogInfo info)
{
if(list.Count<max)
list.Add(info);
else
this.Save();
} public bool Save()
{
//实现根据list日志写入。
return false;
} #endregion
}
是把所有的都加起来,而你没有把它清空
所以
g.WriteLine(Convert.ToString(HttpContext.Current.Application["ComLogRecord"]);
这句就是把前面的1000条记录写进去了,可以在else中
HttpContext.Current.Application["ComLogRecord"] =log;
然后再
g.WriteLine(Convert.ToString(HttpContext.Current.Application["ComLogRecord"]);