多线程中使用HttpContext.Current为null的解决办法
HttpContext.Current.Server.MapPath(logFile) 这个是得到具体路径的方法 正常情况下是可以的 多线程情况下就为null
下边的代码原本的作用是把网站的异常错误信息写入log.txt中
这里抽出部分代码是我测试System.Timers.Timer的
把网站的异常错误信息写入log.txt的原代码在这里:http://www.cnblogs.com/0banana0/archive/2012/05/04/2483246.html

public static void LogException(Exception exc, string source)
{ string logFile = "App_Data/ErrorLog.txt"; //多线程的话HttpContext.Current这个会为null就执行else里边的东东
if (HttpContext.Current != null)
{
logFile = HttpContext.Current.Server.MapPath(logFile);
}
else
{
//多线程执行这里
logFile = logFile.Replace("/", "\\");
if (logFile.StartsWith("\\"))//确定 String 实例的开头是否与指定的字符串匹配。为下边的合并字符串做准备
{
logFile = logFile.TrimStart('\\');//从此实例的开始位置移除数组中指定的一组字符的所有匹配项。为下边的合并字符串做准备
}
//AppDomain表示应用程序域,它是一个应用程序在其中执行的独立环境
//AppDomain.CurrentDomain 获取当前 Thread 的当前应用程序域。
//BaseDirectory 获取基目录,它由程序集冲突解决程序用来探测程序集。
//AppDomain.CurrentDomain.BaseDirectory综合起来就是返回此代码所在的路径
//System.IO.Path.Combine合并两个路径字符串
//Path.Combine(@"C:\11","aa.txt") 返回的字符串路径如后: C:\11\aa.txt
logFile=System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, logFile);
} // Open the log file for append and write the log
StreamWriter sw = new StreamWriter(logFile, true);
sw.Write("******************** " + DateTime.Now);
sw.WriteLine(" ********************");
if (exc == null)
{
sw.Write(source);
} sw.WriteLine();
sw.Close();
}

Global.aspx里面的代码如下

void Application_Start(object sender, EventArgs e)
{
//在应用程序启动时运行的代码 System.Timers.Timer t = new System.Timers.Timer(1000);
t.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
t.AutoReset = true;
t.Enabled = true;
}
private static void OnTimedEvent(object sender, EventArgs e)
{
ExceptionUtility.LogException(null, "Timer: Hello World");
}

网站启动后没隔一秒给log.txt中写入一次
******************** 2012/5/11 19:19:35 ********************
Timer: Hello World
这个只是简单的示例介绍timer的 以及遇到多线程HttpContext.Current为null的解决办法
解决办法在这里找到的:http://topic.csdn.net/u/20090103/15/4e8b403e-5dfd-4afd-a364-1c38a18e5a03.html
多线程中使用HttpContext.Current为null的解决办法的更多相关文章
- WCF Service中HttpContext.Current为null的解决办法
1. 在hosting WCF的web.config中加入: <system.serviceModel> <serviceHostingEnvironment aspNetCompa ...
- ASP.NET多线程下使用HttpContext.Current为null解决方案 2015-01-22 15:23 349人阅读 评论(0) 收藏
问题一:多线程下获取文件绝对路径 当我们使用HttpContext.Current.Server.MapPath(strPath)获取绝对路径时HttpContext.Current为null,解决办 ...
- ASP.NET多线程下使用HttpContext.Current为null解决方案 2015-01-22 15:23 350人阅读 评论(0) 收藏
问题一:多线程下获取文件绝对路径 当我们使用HttpContext.Current.Server.MapPath(strPath)获取绝对路径时HttpContext.Current为null,解决办 ...
- ASP.NET多线程下使用HttpContext.Current为null解决方案
多线程或者异步调用中如何访问HttpContext? 前面我还提到在APM模式下的异步完成回调时,访问HttpContext.Current也会返回null,那么此时该怎么办呢? 答案有二种:1. 在 ...
- 多线程中遇到ASSERT(pMap->LookupPermanent(hWndOrig) == NULL);怎么解决
XP下用VC开发的程序,在一个主线程调用3 个线程,线程之间要共享数据,结果总出现wincore.cpp line 980 ASSERT(pMap-> LookupPermane ...
- ASP.NET多线程下使用HttpContext.Current
本来要实现asp.net下使用tcp通讯方式向服务器获取数据,开始采用的方式是 参考: ASP.NET多线程下使用HttpContext.Current为null解决方案 http://www.cnb ...
- Spring MVC普通类或工具类中调用service报空空指针的解决办法(调用service报java.lang.NullPointerException)
当我们在非Controller类中应用service的方法是会报空指针,如图: 这是因为Spring MVC普通类或工具类中调用service报空null的解决办法(调用service报java.la ...
- 解决Asp.net Mvc中使用异步的时候HttpContext.Current为null的方法
在项目中使用异步(async await)的时候发现一个现象,HttpContext.Current为null,导致一系列的问题. 上网查了一些资料后找到了一个对象: System.Threading ...
- .net webapi 中使用session是出错 HttpContext.Current.Session==null
最近在写.net webapi时发现 HttpContext.Current.Session==null ,导致报错,后来查资料发现webapi中使用session时首先需要开启session功能, ...
随机推荐
- springboot2.0+Neo4j+d3.js构建知识图谱
Welcome to the Neo4j wiki! 初衷这是一个知识图谱构建工具,最开始是对产品和领导为了做ppt临时要求配合做图谱展示的不厌其烦,做着做着就抽出一个目前看着还算通用的小工具 技术栈 ...
- jdk tomcat
vi /etc/profile export JAVA_HOME=/usr/java/jdk1.8.0_121export JRE_HOME=/usr/java/jdk1.8.0_121/jreexp ...
- LDAP相关操作注意事项
lc.Modify(entry.DN, new LdapModification(LdapModification.REPLACE, new LdapAttribute("mDBUseDef ...
- 如何将拷贝过来的数据 *.ibd 文件生效
1.将拷贝的数据文件 "qqq.idb"放在自己的数据库中. 一般存放在 mysql/ data/ databasename 下 2. "qqq.idb" ...
- (转)CentOS 7系统详细开机启动流程和关机流程
CentOS 7系统详细开机启动流程和关机流程 原文:http://blog.csdn.net/yuesichiu/article/details/51350654 名称 bootup - 系统启动流 ...
- (转)linux磁盘分区fdisk分区和parted分区
linux磁盘分区fdisk分区和parted分区 原文:http://www.cnblogs.com/jiu0821/p/5503660.html ~~~~~~~~~~~~~~~~~~~~~~~~~ ...
- Oracle的表操作,约束
回顾MySQL创建表语句users(id整型/name字符串/birthday日期型,默认今天)drop table if exists users;create table if not exist ...
- JavaSE_4_集合
1.Map和ConcurrentHashMap的区别? Map和ConcurrentHashMap的区别,Map是接口,ConcurrentHashMap是实现类 2.hashMap内部具体如何实现的 ...
- linux解压与参数介绍
linux下 各种解压文件使用方法:https://www.jianshu.com/p/ca41f32420d6 解压参数详解:http://www.cnblogs.com/jyaray/archiv ...
- weex 项目开发 weexpack 项目 打包、签名、发布
一. weexpack build android 和 weexpack run android 的 区别. (1)单纯打包 weexpack build android (2)打包并运行 wee ...