C # log class, utility. NET Log Action class

Source: Internet
Author: User
Tags save file

C # log class, utility. NET Log Action class

System logs are frequently used by both Web applications and Windows Forms applications. Log can help us to track the operation of the monitoring system, timely detection of errors, output mode information. There are many ways to log logs, such as using text files, XML files, databases, and so on. Logging with a text file is one of the most common methods.

Here is a simple and useful log class with a text file to log, and it has several features as follows:

1 daily production of different log files by date, convenient to find the log according to the date.

2 According to the type of log production of different files, such as tracking information, warning messages, error messages with different log files to record, to facilitate us to find the specified type of log.

3 The log file folder can be specified, if the log folder is not specified, the Web application is persisted to the Bin folder, and the Windows forms application remains in the same folder as the. exe file.

4) You can specify the prefix of the log file.

public class Logmanager

{

private static string LogPath = String. Empty;

<summary>

folder where log is saved

</summary>

Publicstatic string LogPath

{

Get

{

if (LogPath = = string. Empty)

{

if (System.Web.HttpContext.Current = null)

Windows Forms Application

LogPath = AppDomain.CurrentDomain.BaseDirectory;

Else

Web applications

LogPath = AppDomain.CurrentDomain.BaseDirectory + @ "bin\";

}

return logPath;

}

Set{logpath = value;}

}

Privatestatic string logfielprefix = string. Empty;

<summary>

Log file Prefix

</summary>

Publicstatic string Logfielprefix

{

get {return logfielprefix;}

set {Logfielprefix = value;}

}

<summary>

Write log

</summary>

publicstatic void Writelog (String logFile, String msg)

{

Try

{

System.IO.StreamWriter SW = System.IO.File.AppendText (

LogPath + logfielprefix + logFile + "" +

DateTime.Now.ToString ("yyyyMMdd") + ". Log "

);

Sw. WriteLine (DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss:") +msg);

Sw. Close ();

}

Catch

{ }

}

<summary>

Write log

</summary>

publicstatic void Writelog (LogFile LogFile, String msg)

{

Writelog (Logfile.tostring (), MSG);

}

}

<summary>

Log type

</summary>

public enum LogFile

{

Trace,

Warning,

Error,

Sql

}

How to use:

Logmanager.logfielprefix = "ERP";

Logmanager.logpath = @ "C:\";

Logmanager.writelog (Logfile.trace, "A test Msg.");



· C # Read and write text files, binary files

2008-03-25

Copyright notice: When reprinted, please indicate the original source and author information of the article by hyperlink form and this statement
Http://q-hj.blogbus.com/logs/42336562.html

C # operation files

All rights reserved: Tianshan Cold Snow qq:757015000 msn:haijun.qin@hotmail.com

}
<summary>
Reading binary files
</summary>
<param name= "filename" ></param>
private void Readbinaryfiles (string filename)
{
FileStream Filesstream = new FileStream (filename, filemode.create);
BinaryWriter objbinarywriter = new BinaryWriter (filesstream);
for (int index = 0; index < index++)
{
Objbinarywriter.write ((int) index);
}
Objbinarywriter.close ();
Filesstream. Close ();
}
<summary>
Writing binary files
</summary>
<param name= "filepath" ></param>
private void Writebinaryfiles (string filepath)
{
if (! File.exists (filepath))
{
File does not exist
}
Else
{
FileStream FileStream = new FileStream (filepath, filemode.open,fileaccess.read);
BinaryReader objbinaryreader = new BinaryReader (FILESTREAM);
Try
{
while (true)
{
Objbinaryreader.readint32 ();
}
}
catch (Exception ex)
{
End of file already in
}
}
}

File class:
Create (String FilePath)
Creates a file with the specified name under the specified path. This method returns a FileStream object
OpenRead (String FilePath)
Open an existing file to read data
AppendText (String Filepah)
Allow text to be added to text
Copy (String Sourcefilepath,stringdestinationpath)
Copies the contents of the source file to the destination file by the specified path. If the target does not exist, a new file is created with the specified name in the specified path
Delete (String FilePath)
Deletes a file of the specified path
Exists (String FilePath)
Verifies that a file with the specified name exists in the specified path. This method returns a Boolean value

<summary>
Write to File
</summary>
<param name= "filename" ></param>
<param name= "Filecontent" ></param>
private void SaveFiles (String filename,string filecontent)
{
FileStream FS;
Try
{
FS = file.create (filename);
}
catch (Exception ex)
{
Throw ex;
}
byte[] content = new UTF8Encoding (true). GetBytes (filecontent);
Try
{
Fs. Write (content, 0, content. Length);
Fs. Flush ();
}
catch (Exception ex)
{
Throw ex;
}
Finally
{
Fs. Close ();
}
}
<summary>
Save File
</summary>
<param name= "Path" ></param>
private void Readfiles (string path)
{
Try
{
if (! File.exists (PATH))
{
File does not exist
}
Else
{
Open stream Read
FileStream fs = file.openread (path);
Creates a byte array to read data
byte[] arr = new byte[100];
UTF8Encoding data = new UTF8Encoding (true);
Continue reading all the data of the file
while (FS. Read (arr, 0, arr. Length) > 0)
{
Data. GetString (arr);
}
}
}
catch (Exception ex)
{
Throw ex;
}

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.