
前言
老大说希望在项目中加一个日志功能,目前的项目是一个多线程轮询的架构。添加日志需要获取文件绝对路径,这时发现 HttpContext.Current 为 null。
解决方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
public static string MapPath(string strPath) { if (HttpContext.Current != null) { return HttpContext.Current.Server.MapPath(strPath); } else { strPath = strPath.Replace("/", "\"); if (strPath.StartsWith("\")) { strPath = strPath.TrimStart('\'); } return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath); } }
|
近期评论