频道栏目
首页 > 资讯 > 其他综合 > 正文

常见.NET功能代码汇总 (2)

16-07-08        来源:[db:作者]  
收藏   我要投稿

23,获取和设置分级缓存

获取缓存:首先从本地缓存获取,如果没有,再去读取分布式缓存
写缓存:同时写本地缓存和分布式缓存

  private static T GetGradeCache(string key) where T:struct 
        {
            MemoryCacheManager localCache = MemoryCacheManager.Instance;
            if (!localCache.IsSet(key))
            {
                //本地不存在此缓存
               T  remoteValue = MemCacheManager.Instance.Get(key);
               if (!ValueType.Equals(remoteValue, default(T)))
               { 
                    //如果远程有
                   localCache.Set(key, remoteValue, 1); 
               }
               else
               {
                   localCache.SetFromSeconds(key, default(T), 10); 
               }
               return remoteValue;
            }
            T value = localCache.Get(key);
            return value;
        }

        private static void SetGradeCache(string key,T Value,int time) where T : struct 
        {
            MemoryCacheManager localCache = MemoryCacheManager.Instance;
            localCache.Remove(key);
            localCache.Set(key, Value, time);
            MemCacheManager.Instance.Remove(key);
            MemCacheManager.Instance.Set(key, Value, time); 
        }
24,求相对目录的绝对路径

有时候,我们需要求相对于当前根目录的相对目录,比如将日志文件存储在站点目录之外,我们可以使用 ../logs/ 的方式:

 string vfileName = string.Format("../logs/{0}_{1}_{2}.log", logFileName, System.Environment.MachineName, DateTime.Now.ToString("yyyyMMdd"));
            string rootPath = HttpContext.Current.Server.MapPath("/");
            string targetPath = System.IO.Path.Combine(rootPath, vfileName);
            string fileName = System.IO.Path.GetFullPath(targetPath);
            string fileDir = System.IO.Path.GetDirectoryName(fileName);
            if (!System.IO.Directory.Exists(fileDir))
                System.IO.Directory.CreateDirectory(fileDir);

这个代码会在站点目录之外的日志目录,建立一个 代机器名称的按照日期区分的日志文件。

25,多次尝试写日志文件方法

日志文件可能会并发的写入,此时可能会提示“文件被另外一个进程占用”,因此可以多次尝试写入。下面的方法会递归的进行文件写入尝试,如果尝试次数用完才会最终报错。

  /// 
        /// 保存日志文件
        /// 
        ///不带扩展名文件名
        ///日志内容
        ///如果出错的尝试次数,建议不大于100,如果是0则不尝试
        public static void SaveLog(string logFileName, string logText, int tryCount)
        {
            string vfileName = string.Format("..\\logs\\{0}_{1}_{2}.log", logFileName, System.Environment.MachineName, DateTime.Now.ToString("yyyyMMdd"));
            string rootPath = System.AppDomain.CurrentDomain.BaseDirectory;
            string targetPath = System.IO.Path.Combine(rootPath, vfileName);
            string fileName = System.IO.Path.GetFullPath(targetPath);
            string fileDir = System.IO.Path.GetDirectoryName(fileName);
            if (!System.IO.Directory.Exists(fileDir))
                System.IO.Directory.CreateDirectory(fileDir);
            
            try
            {
                System.IO.File.AppendAllText(fileName, logText);
                tryCount = 0;
            }
            catch (Exception ex)
            {
                if (tryCount > 0)
                {
                    System.Threading.Thread.Sleep(1000);
                    logText = logText + "\r\nSaveLog,try again times =" + tryCount + " ,Error:" + ex.Message;
                    tryCount--;
                    SaveLog(logFileName, logText, tryCount);
                }
                else
                {
                    throw new Exception("Save log file Error,try count more times!");
                }
            }
        }

 

相关TAG标签
上一篇:csharp: Procedure with DAO(Data Access Object) and DAL(Data Access Layer)
下一篇:MVC 5 中Filter控制 action的访问权限
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站