Solution for using httpcontext. Current in multithreading as null

Source: Internet
Author: User

Httpcontext. Current. server. mappath (logfile). This is the method for obtaining the specific path. Normally, it is possible to use multiple threads, and the value is null.

The source code of the lower-end code is to write abnormal and error information of the website to log.txt.

Some code is extracted here to test system. Timers. Timer

The original code to write the abnormal error information of the website to log.txt here: 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 for multithreading. if the value is null, the East-East if (httpcontext. current! = NULL) {logfile = httpcontext. current. server. mappath (logfile);} else {// multi-thread Execution here logfile = logfile. replace ("/", "\"); If (logfile. startswith ("\") // determines whether the start of the string instance matches the specified string. Prepare the following string for merging {logfile = logfile. trimstart ('\'); // remove all matching items for the specified group of characters in the array from the start position of the instance. Prepare for the following merge strings} // appdomain indicates the application domain, which is an independent environment in which an application executes // appdomain. currentdomain gets the current application domain of the current thread. // Basedirectory gets the base Directory, which is used by the Assembly conflict resolution program to detect the assembly. // Appdomain. currentdomain. in combination, basedirectory returns the path of the Code. // system. io. path. combine merges two path strings // path. the string path returned by combine (@ "C: \ 11", "aa.txt") is 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 ();}

 

The code in global. aspx is as follows:

Void application_start (Object sender, eventargs e) {// code system that runs when the application starts. 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 ");}

 

The website is not written to log.txt every second after it is started.

******************* 19:19:35 ****************** **
Timer: Hello World

 

 

This is just a simple example to introduce timer and the solution to multi-threaded httpcontext. Current is null.

Solution found here: http://topic.csdn.net/u/20090103/15/4e8b403e-5dfd-4afd-a364-1c38a18e5a03.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.