我编写了一个记录访问我网站的信息的日志,我会把1000条前先放在Application缓存中,当超过1000我就会写进日志,这样一开始好好的,但是会在某个时候突然日志很大,发现日志内有很大部分是重复的数据,那肯定是缓存没有获取的新的信息,而且也没有清空原来的数据,造成不断把缓存中的数据写入日志文件,这样也让我丢失了那些时段的数据,只有重启IIS才恢复,我想问下为什么会出现这样的情况,缓存不变了是什么原因?

解决方案 »

  1.   

    代码入下
    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;
    }
      

  2.   

    去google一下吧,singleton关键字觉得你的做法很奇怪呢
      

  3.   

    自己写一个日志类,来处理你的日志。
    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;
    }
    }
      

  4.   

    但是我还没高清楚为什么Application里面的值不会被更改
      

  5.   

    接口补充下
    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
    }
      

  6.   

    HttpContext.Current.Application["ComLogRecord"] = Convert.ToString(HttpContext.Current.Application["ComLogRecord"]) + log;
    是把所有的都加起来,而你没有把它清空
    所以
    g.WriteLine(Convert.ToString(HttpContext.Current.Application["ComLogRecord"]);
    这句就是把前面的1000条记录写进去了,可以在else中
    HttpContext.Current.Application["ComLogRecord"] =log;
    然后再
    g.WriteLine(Convert.ToString(HttpContext.Current.Application["ComLogRecord"]);
      

  7.   

    else里面的log已经是新的了,我也把记录条数设成1了,写也是把上次的1000条写进日志,然后重新开始记录啊