Simple Log Management Code and simple log management code

Source: Internet
Author: User

Simple Log Management Code and simple log management code

You can regularly delete logs by writing your own logs.

The method is relatively simple. record it.

/// <Summary> /// write logs /// </summary> /// <param name = "strMsg"> content </param> /// <param name = "strPath"> path (relative to the folder path under hycom) </param> // <param name = "fileName"> log name (~yyyy-mm-dd.txt) </param> public static void WriteLog (string strMsg, string strPath, string fileName) {string path = AppDomain. currentDomain. baseDirectory + strPath; if (! Path. EndsWith ("\") |! Path. EndsWith ("/") {path + = "\";} if (! Directory. exists (path) {Directory. createDirectory (path);} DeleteLog (path); if (fileName = "") {fileName = DateTime. now. toString ("yyyy-MM-dd") + ". txt ";} if (! FileName. endsWith (". txt ") {fileName + = ". txt ";} try {// string fileName = DateTime. now. toString ("yyyy-MM-dd") + ". txt "; StreamWriter sw = File. appendText (path + fileName); sw. writeLine ("{0 }:{ 1}", DateTime. now. toString ("yyyy-MM-dd HH: mm: ss: fff"), strMsg); // sw. writeLine ("\ n \ r"); sw. flush (); sw. close ();} catch {}}/// <summary> /// write logs /// </summary> /// <param name = "strMsg"> content </param> /// <param name = "strPath"> path (relative to the folder path under hycom) </param> public static void WriteLog (string strMsg, string strPath) {string fileName = DateTime. now. toString ("yyyy-MM-dd") + ". txt "; WriteLog (strMsg, strPath, fileName );} /// <summary> /// regularly delete logs /// </summary> /// <param name = "strPath"> Log Path </param> private static void DeleteLog (string strPath) {if (Directory. exists (strPath) {DirectoryInfo dinfor = new DirectoryInfo (strPath); FileInfo [] files = dinfor. getFiles (); foreach (FileInfo file in files) {try {// Delete the log created three months ago if (DateTime. compare (file. creationTime. addMonths (3), DateTime. now) <0) {file. delete ();} // Delete the log that was last modified three months ago if (DateTime. compare (file. lastWriteTime. addMonths (3), DateTime. now) <0) {file. delete ();} // Delete the log name if (DateTime. compare (Convert. toDateTime (file. name. substring (0, 10), DateTime. now. addMonths (-3) <0) {file. delete () ;}} catch (Exception ex) {continue ;}}}}

  

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.