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的解决办法的更多相关文章

  1. WCF Service中HttpContext.Current为null的解决办法

    1. 在hosting WCF的web.config中加入: <system.serviceModel> <serviceHostingEnvironment aspNetCompa ...

  2. ASP.NET多线程下使用HttpContext.Current为null解决方案 2015-01-22 15:23 349人阅读 评论(0) 收藏

    问题一:多线程下获取文件绝对路径 当我们使用HttpContext.Current.Server.MapPath(strPath)获取绝对路径时HttpContext.Current为null,解决办 ...

  3. ASP.NET多线程下使用HttpContext.Current为null解决方案 2015-01-22 15:23 350人阅读 评论(0) 收藏

    问题一:多线程下获取文件绝对路径 当我们使用HttpContext.Current.Server.MapPath(strPath)获取绝对路径时HttpContext.Current为null,解决办 ...

  4. ASP.NET多线程下使用HttpContext.Current为null解决方案

    多线程或者异步调用中如何访问HttpContext? 前面我还提到在APM模式下的异步完成回调时,访问HttpContext.Current也会返回null,那么此时该怎么办呢? 答案有二种:1. 在 ...

  5. 多线程中遇到ASSERT(pMap->LookupPermanent(hWndOrig) == NULL);怎么解决

    XP下用VC开发的程序,在一个主线程调用3   个线程,线程之间要共享数据,结果总出现wincore.cpp   line   980   ASSERT(pMap-> LookupPermane ...

  6. ASP.NET多线程下使用HttpContext.Current

    本来要实现asp.net下使用tcp通讯方式向服务器获取数据,开始采用的方式是 参考: ASP.NET多线程下使用HttpContext.Current为null解决方案 http://www.cnb ...

  7. Spring MVC普通类或工具类中调用service报空空指针的解决办法(调用service报java.lang.NullPointerException)

    当我们在非Controller类中应用service的方法是会报空指针,如图: 这是因为Spring MVC普通类或工具类中调用service报空null的解决办法(调用service报java.la ...

  8. 解决Asp.net Mvc中使用异步的时候HttpContext.Current为null的方法

    在项目中使用异步(async await)的时候发现一个现象,HttpContext.Current为null,导致一系列的问题. 上网查了一些资料后找到了一个对象: System.Threading ...

  9. .net webapi 中使用session是出错 HttpContext.Current.Session==null

    最近在写.net webapi时发现 HttpContext.Current.Session==null  ,导致报错,后来查资料发现webapi中使用session时首先需要开启session功能, ...

随机推荐

  1. 毕马威&阿里:通向智能制造的转型之路

    文章发布于公号[数智物语] (ID:decision_engine),关注公号不错过每一篇干货. 2019 年 4 月 17 日,毕马威与阿里研究院携手举办了智能经济主题报告发布会,从技术.制造.组织 ...

  2. Django框架base.py源码

    url.py文件 from django.conf.urls import url from django.contrib import admin from app_student import v ...

  3. redmine迁移

    (redmine使用的是bitnamiredmine一键安装,环境为apache+mysql+php) 在新环境中一键安装bitnamiredmine,安装完后,执行下面操作 1.备份原环境数据库,恢 ...

  4. GUI的最终选择 Tkinter(五):Text用法

    Text组件 绘制单行文本使用Label组件,多行选使用Listbox,输入框使用Entry,按钮使用Button组件,还有Radiobutton和Checkbutton组件用于提供单选或多选的情况, ...

  5. Java集合——集合框架Map接口

    1.Map接口 public interface Map<K,V>将键映射到值的对象.一个映射不能包含重复的键:每个键最多只能映射到一个值.  2.HashMap.Hashtable.Tr ...

  6. serv-U 7以上版本pasv端口的设置及中文乱码问题

    利用serv-u架设ftp服务器已经是再常见不过了事情了,近日一朋友为图新鲜,弄了个7.4版本的新玩意儿,结果架设上去后,仅开了21端口,用LeapFtp在port模式下连接没问题,但是另一常见的cu ...

  7. spring mvc 注解扫描问题 ,扫描不到controller, use-default-filters="false"

    今天搭了个spring mvc项目,怎么也扫描不到controller,最后发现问题在use-default-filters="false"上面,乱copy出的问题 (默认值是tr ...

  8. 《C#高效编程》读书笔记07-理解GetHashCode()的陷阱

    GetHashCode()函数仅会在一个地方用到,即为基于散列(hash)的集合定义的散列键时,此类集合包括HashSet和Dictionary<K,V>容器等. 但object基类提供的 ...

  9. log4net 最快速体验

    本文供实习司机快速上手log4net最基本功能,共4步,3分钟搞定. 一.添加log4net.dll引用,可使用nuget安装或直接引用文件 二.添加配置 在app.config或web.config ...

  10. 【问题记录】mysql TIMEDIFF 和 TIMESTAMPDIFF的使用

    今天遇到一个需求,需要计算数据表中两个时间的差值,并取对应的秒数 一开始我是用 time_to_sec(timediff (time1,time2)) 但是这样会有一个问题,,,时间短的用这个计算没有 ...