Asp.net Method for logging error logs, asp.net error logs

Source: Internet
Author: User

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.

Related Article

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.