StreamWriter sw = new StreamWriter
(HttpContext.Current.Server.MapPath("~/ExceptionLog/LogFile.txt"),true);HttpContext是一个类,Current是它的属性,后面Server表示什么呢,MapPath是路径,单个可以理解,但是整体上看不懂。
请指教!
(HttpContext.Current.Server.MapPath("~/ExceptionLog/LogFile.txt"),true);HttpContext是一个类,Current是它的属性,后面Server表示什么呢,MapPath是路径,单个可以理解,但是整体上看不懂。
请指教!
这句只是得到这个文件在服务器上的绝对路径
将虚拟路径转换成实际路径,
HttpContext.Current.Server.MapPath
我查MSDN看的HttpContext是一个密封类,Server和Current是属性,类后面怎么加两个属性呢?
完整代码如下 :
public static void LogException( Exception ex )
{
using ( StreamWriter sw = new StreamWriter( HttpContext.Current.Server.MapPath( @"ExceptionLog\LogFile.txt" ) , true ) )
{
sw.WriteLine( DateTime.Now.ToShortDateString() + Environment.NewLine + ex.InnerException.ToString() + Environment.NewLine + Environment.NewLine );
}
}
}
}它的作用是将异常记录到文件中,我不明白的是HttpContext.Current.Server.MapPath
这种写法,谢谢指教!
HttpContext是一个密封类,Current属性返回一个 HttpContext 对象,返回的对象有Server 属性,
该属性获取提供用于处理 Web 请求的方法的 HttpServerUtility 对象。 HttpServerUtility 对象有
MapPath 方法,该方法返回与 Web 服务器上的指定虚拟路径相对应的物理文件路径,参数是虚拟路径。
总的来说就是它可以读取代码隐藏模块中的 MapPath
挺乱的,谢谢各位了。