我的代码如下 求高手哇哇
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class index : System.Web.UI.Page
{
protected string strError = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
strFileError("刷新时间:" + DateTime.Now.ToString("hh:mm:ss"), true);
CacheItemUpdateCallback onUpdate = new CacheItemUpdateCallback(UpdataCallback);
//30秒后就到期,立即移除
HttpRuntime.Cache.Insert("DD", "绝对过期测试", null, DateTime.Now.AddSeconds(5), System.Web.Caching.Cache.NoSlidingExpiration, onUpdate);
}
//缓存回调函数
private static void UpdataCallback(string key, CacheItemUpdateReason reason, out object expensiveObject, out CacheDependency dependency, out DateTime absoluteExpiration, out TimeSpan slidingExpiration)
{
try
{
strFileError("调用时间:" + DateTime.Now.ToString("hh:mm:ss"), false);
expensiveObject = DateTime.Now.ToString();
}
catch (Exception)
{
expensiveObject = DateTime.Now.ToString();
}
dependency = null;
absoluteExpiration = DateTime.UtcNow.AddSeconds(5);
slidingExpiration = Cache.NoSlidingExpiration;
}
public static void strFileError(string ss, bool isFirst = false) {
StreamWriter sr = null;
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "File");
if (!Directory.Exists(filePath)) {
Directory.CreateDirectory(filePath);
}
string fullName = filePath + "/error.text";
if(isFirst){
sr = File.CreateText(fullName);
}else
{
sr = File.AppendText(fullName);
}
if(isFirst){
sr.WriteLine("开始记录:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}else
{
sr.WriteLine(ss);
}
sr.Close();
}
}
}