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. JSP-模拟银行卡账号密码登录页面跳转

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  2. [USACO14MAR]破坏Sabotage 二分答案

    题目描述 Farmer John's arch-nemesis, Farmer Paul, has decided to sabotage Farmer John's milking equipmen ...

  3. redis使用方法

    redis缓存服务器笔记 redis是一个高性能的key-value存储系统,能够作为缓存框架和队列 但是由于他是一个内存内存系统,这些数据还是要存储到数据库中的 作为缓存框架: create/upd ...

  4. .gitkeep--git提交空目录的解决方法

    前言 git和 svn不同,仅仅跟踪文件的变动,不跟踪目录.所以,一个空目录,如果里面没有文件,即便 git add 这个目录,另外在别处 check out 的时候,是没有这个空目录的. 只跟踪文件 ...

  5. 自动检测GD库支持的图像类型

    以下代码通过自动检测GD库支持的图像类型 来写出移直性更好的PHP代码 <?php if(function_exists("imagegif")){ header(" ...

  6. linux中的三种时间

    mtime [修改时间] 文件/目录的修改时间 ctime  [属性修改时间] 文件目录的属性的修改时间 atime  [访问时间]文件/目录的访问时间 stat 123.txt   File: `1 ...

  7. JS——定时器

    定时器在JS中的作用: 1)制作动画.时钟.倒计时 2)异步操作 3)函数缓冲与节流 定时器类型: 1)setTimeout 只执行一次的定时器 2)clearTimeout 关闭只执行一次的定时器 ...

  8. varnish与squid的比较

    生产环境中尝试使用varnish替代squid的主要原因: 1. squid不支持多核cpu, 生产环境中大多使用Dell R610系列,这种类型机器配置为2个4核双线程cpu, 操作系统识别为16个 ...

  9. php函数超实用

    DateTime DateTime::addDateTime::diffDateTime::formatDateTime::modifyDateTime::sub... * DateInterval ...

  10. SpringMVC07SelfException 自定义异常

    1.配置web.xml文件 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3// ...