1
public
void
WriteLog(Exception ex)
2 {
3 try
4 {
5 String FolderPath = ConfigurationManager.AppSettings[ "" ];
6
7 string _path = "" ;
8 if ( ! IsPhysicalPath(FolderPath))
9 _path = HttpContext.Current.Server.MapPath(FolderPath);
10 else _path = FolderPath;
11
12 System.IO.DirectoryInfo dinfo = new DirectoryInfo(_path);
13
14 if ( ! dinfo.Exists)
15 dinfo.Create();
16 String filePath = _path + DateTime.Now.ToShortDateString() + " _Log.log " ;
17
18 String logStr = String.Empty;
19 logStr += " 发生时间: " + DateTime.Now.ToString() + " \r\n " ;
20 logStr += " Requested address: " + HttpContext.Current.Request.RawUrl + " \ r \ the n- " ; 21 logStr + = " Source Address: " + ((HttpContext.Current.Request.UrlReferrer == null ) ? " Url address bar directly input " : HttpContext.Current.Request.UrlReferrer.AbsoluteUri) + " \ R & lt \ n- " ; 22 is logStr + = " visitor P the I: " + HttpContext.Current.Request.UserHostAddress + "\r\n"
;
23 is logStr + = " Error Class Name: " + occurClass + " \ R & lt \ n- ' ; 24 logStr + = " Error Method: " + occurMethod + " \ R & lt \ n- ' ; 25 logStr + = " Description: " + ex.Message + " \ R & lt \ n- " ; 26 is logStr + = " trace information: " + EX.StackTrace+"
\ R & lt \ n- " ;
27 logStr + = " --------------------------------------- -------------------------------------------------- ------------------------ \ R & lt \ n-\ R & lt \ n-\ R & lt \ n- " ; 28 File.AppendAllText (filePath, logStr, the System.Text .Encoding.Unicode); 29 } 30 the catch 31 is {} 32 } 33 is // to true: absolute path; false: relative path 34
public static bool IsPhysicalPath( string Path)
35 {
36 string RegexString = @" \b[a-z]:\\.* " ;
37 MatchCollection Matchs = Regex.Matches(Path, RegexString, RegexOptions.IgnoreCase | RegexOptions.Compiled);
38 if (Matchs.Count > 0 )
39 return true ;
40 return false ;
41 }
2 {
3 try
4 {
5 String FolderPath = ConfigurationManager.AppSettings[ "" ];
6
7 string _path = "" ;
8 if ( ! IsPhysicalPath(FolderPath))
9 _path = HttpContext.Current.Server.MapPath(FolderPath);
10 else _path = FolderPath;
11
12 System.IO.DirectoryInfo dinfo = new DirectoryInfo(_path);
13
14 if ( ! dinfo.Exists)
15 dinfo.Create();
16 String filePath = _path + DateTime.Now.ToShortDateString() + " _Log.log " ;
17
18 String logStr = String.Empty;
19 logStr += " 发生时间: " + DateTime.Now.ToString() + " \r\n " ;
20 logStr += " Requested address: " + HttpContext.Current.Request.RawUrl + " \ r \ the n- " ; 21 logStr + = " Source Address: " + ((HttpContext.Current.Request.UrlReferrer == null ) ? " Url address bar directly input " : HttpContext.Current.Request.UrlReferrer.AbsoluteUri) + " \ R & lt \ n- " ; 22 is logStr + = " visitor P the I: " + HttpContext.Current.Request.UserHostAddress + "\r\n"
;
23 is logStr + = " Error Class Name: " + occurClass + " \ R & lt \ n- ' ; 24 logStr + = " Error Method: " + occurMethod + " \ R & lt \ n- ' ; 25 logStr + = " Description: " + ex.Message + " \ R & lt \ n- " ; 26 is logStr + = " trace information: " + EX.StackTrace+"
\ R & lt \ n- " ;
27 logStr + = " --------------------------------------- -------------------------------------------------- ------------------------ \ R & lt \ n-\ R & lt \ n-\ R & lt \ n- " ; 28 File.AppendAllText (filePath, logStr, the System.Text .Encoding.Unicode); 29 } 30 the catch 31 is {} 32 } 33 is // to true: absolute path; false: relative path 34
public static bool IsPhysicalPath( string Path)
35 {
36 string RegexString = @" \b[a-z]:\\.* " ;
37 MatchCollection Matchs = Regex.Matches(Path, RegexString, RegexOptions.IgnoreCase | RegexOptions.Compiled);
38 if (Matchs.Count > 0 )
39 return true ;
40 return false ;
41 }
Reproduced in: https: //www.cnblogs.com/TSPWater/archive/2010/08/27/1809995.html