Asp.net Method for logging error logs, asp.net error logs
1. Description
Unexpected errors may occur in the asp.net project after debugging and release, but they cannot be displayed in a timely manner. This requires logging to track error information. Therefore, we have written a simple method to record information, which can also be used to record simple text information. This method is recorded by generating a text file. The following code is pasted:
2. Code
Need to referenceUsing System. IO;
Byte [] myByte = System. Text. Encoding. UTF8.GetBytes ("here is the Text of the record you want"); string strPath = Server. MapPath ("~ ") +" \ Log \ "; if (! Directory. exists (strPath) {Directory. createDirectory (strPath);} string strPathLog = strPath + DateTime. now. toString ("yyyyMMddHHmmss") + ". txt "; using (FileStream fsWrite = new FileStream (strPathLog, FileMode. append) {fsWrite. write (myByte, 0, myByte. length );};
A Log folder is generated in the root directory of the project.
3. Example
try { int i = Convert.ToInt32("");}catch (Exception ex) { byte[] myByte = System.Text.Encoding.UTF8.GetBytes(ex.ToString()); string strPath = Server.MapPath("~") + "\\ErrorLog\\"; if (!Directory.Exists(strPath)) { Directory.CreateDirectory(strPath); } string strPathLog = strPath + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt"; using (FileStream fsWrite = new FileStream(strPathLog, FileMode.Append)) { fsWrite.Write(myByte, 0, myByte.Length); };}
Author: Paths QQ: 2490024434
Source: http://www.cnblogs.com/lengzhan/
This article is copyrighted by [Cold War] And the blog Park. You are welcome to repost it to your favorites. You must retain this statement without the author's consent. Otherwise, you will be entitled to pursue legal liability.