多线程中使用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
![](https://common.cnblogs.com/images/copycode.gif)
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();
}
![](https://common.cnblogs.com/images/copycode.gif)
Global.aspx里面的代码如下
![](https://common.cnblogs.com/images/copycode.gif)
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");
}
![](https://common.cnblogs.com/images/copycode.gif)
网站启动后没隔一秒给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功能, ...
随机推荐
- Unity---UNet学习(1)----基本方法介绍
目录 1.Network Manager 2.Network Manager HUD 3.Network Identity 4.Network Transform 5.特性 1.Network Man ...
- [題解](搜索)生日蛋糕(NOI1999)
搜索剪枝, 1.枚舉上下界: 先$R\subset$$(dep,min(\lfloor\sqrt{n-v}\rfloor,lastr-1))$ 后$H\subset$$(dep,min((n-v)/R ...
- PowerDesigner如何将字段的注释显示出来
选定一个编辑的表,右键- >Properties- >Columns- >Customize Columns and Filter(或直接用快捷键Ctrl+U)- >Comme ...
- Python 起步 多版本共存配置
上次我选择的是py2.x,如果我要再装一个py3.x呢 我们去设置环境变量,然后去命令行输入python,这里我故意把环境变量放在第一行,貌似换成3.7了 我们把2.7的放在3.7的前面呢?又换回去了 ...
- maven 引入本地项目jar报红线错误解决方法
问题:本地创建了2个项目,A和B,A引入B,A的pom如下: <dependency> <groupId>com.ebc</groupId> <artifac ...
- mysql主从复制之同步部分库表
这里以mariadb为例,和mysql一样的配置 系统:centos7 主服务器:192.168.0.1:3305(两台服务器都做过时间同步) 从服务器:192.168.0.2:3306(两台服务器都 ...
- VMWare复制虚拟机系统后,模块“Disk”无法启动【转】
1.找到虚拟机所在的目录 将 .vmx文件打开 将文件vmci0.present = "TRUE" 改为 vmci0.present = "FALSE" 2.删 ...
- SpringBoot | 第五章:多环境配置
前言 写上一篇看英文资料,耗费了心力呀,这章,相对来说简单点.也比较熟悉,但是这很实用.不扯了,开始~ 多环境配置 maven的多环境配置 springboot多环境配置 总结 老生常谈 多环境配置 ...
- 用mvc模式,整理前两次的代码并增加登陆注册
简单的servlet连接mysql数据库 使用mvc的登录注册 commons-dbutils-1.6 mysql-connector-java-5.1.40-bin c3p0-0.9.5.2 mch ...
- Ini文件格式说明
http://www.cnblogs.com/CUIT-DX037/ 百度百科介绍:ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储 ...