简单的日志文件操作类 (C#)
2015-04-18 11:44
381 查看
类名称: log;
类方法(公有):
1) log (String) 构造函数,传入完整文件名。
2)bool writeln_log (String) 写入一行日志字符串。
3) static void del_log_file (String) 删除指定文件。
4) bool close_file () 关闭文件
5) (属性) String filepath (读写) 获取或设置文件流打开的完整文件名,若设置则动态改变文件流,使之导向到新的文件。
使用方法:
using lib_ez;
log _log = new log (@"C:\testfile.dat");
_log.writeln_log ("add the first log string...");
_log.writeln_log ("add another log string...");
// redirect stream
_log.filepath = @"C:\another.dat";
_log.writeln_log ("add the first log string to another.dat file...");
// close
_log.close ();
类源代码:
文件: log.cs
/*
author : ez
date : 2015/4/13
describe : a class to support writing log string into logfile
*/
namespace lib_ez {
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class log {
// support dynamically file path changing
public String filepath {
get { return this._logfile; }
set {
if (this._writer != null && close_file ())
this._writer = new StreamWriter (this._logfile = value, true); // support appending writing
else
open_file (this._logfile = value);
}
}
public log (String __log_file) {
this.open_file (this._logfile = __log_file);
}
public bool writeln_log (String __str) {
if (this._writer == null && !this.open_file (this._logfile))
return false;
this._writer.WriteLine (__str);
this._writer.Flush ();
return true;
/*
if (this._fs == null && ! this.open_file (this._logfile))
return false; // error in close file
writer = new StreamWriter (this._fs);
writer.WriteLine (__str);
return true;
*/
}
public static void del_log_file (String __file) {
if (File.Exists (__file))
File.Delete (__file);
}
private bool open_file (String __log_file) {
try {
this._writer = new StreamWriter (__log_file, true);
return true;
}
catch (Exception ex) {
// show exception message to Console
Console.WriteLine ("lib_ez.log.open_file [" + DateTime.Now.ToString () + "] : " + ex.Message);
return false;
}
}
// always return true
public bool close_file () {
this._writer.Dispose ();
return true;
}
private StreamWriter _writer;
private String _logfile;
}
}
类方法(公有):
1) log (String) 构造函数,传入完整文件名。
2)bool writeln_log (String) 写入一行日志字符串。
3) static void del_log_file (String) 删除指定文件。
4) bool close_file () 关闭文件
5) (属性) String filepath (读写) 获取或设置文件流打开的完整文件名,若设置则动态改变文件流,使之导向到新的文件。
使用方法:
using lib_ez;
log _log = new log (@"C:\testfile.dat");
_log.writeln_log ("add the first log string...");
_log.writeln_log ("add another log string...");
// redirect stream
_log.filepath = @"C:\another.dat";
_log.writeln_log ("add the first log string to another.dat file...");
// close
_log.close ();
类源代码:
文件: log.cs
/*
author : ez
date : 2015/4/13
describe : a class to support writing log string into logfile
*/
namespace lib_ez {
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class log {
// support dynamically file path changing
public String filepath {
get { return this._logfile; }
set {
if (this._writer != null && close_file ())
this._writer = new StreamWriter (this._logfile = value, true); // support appending writing
else
open_file (this._logfile = value);
}
}
public log (String __log_file) {
this.open_file (this._logfile = __log_file);
}
public bool writeln_log (String __str) {
if (this._writer == null && !this.open_file (this._logfile))
return false;
this._writer.WriteLine (__str);
this._writer.Flush ();
return true;
/*
if (this._fs == null && ! this.open_file (this._logfile))
return false; // error in close file
writer = new StreamWriter (this._fs);
writer.WriteLine (__str);
return true;
*/
}
public static void del_log_file (String __file) {
if (File.Exists (__file))
File.Delete (__file);
}
private bool open_file (String __log_file) {
try {
this._writer = new StreamWriter (__log_file, true);
return true;
}
catch (Exception ex) {
// show exception message to Console
Console.WriteLine ("lib_ez.log.open_file [" + DateTime.Now.ToString () + "] : " + ex.Message);
return false;
}
}
// always return true
public bool close_file () {
this._writer.Dispose ();
return true;
}
private StreamWriter _writer;
private String _logfile;
}
}
相关文章推荐
- C# 简单的日志文件制作
- 一个简单的C#文件日志组件
- [C/C++]简单的C++日志操作类
- SQL SERVER2005无日志文件附加数据库最简单的方法(转载)
- 终于找到ASP.NET c# 简单的获取远端文件的方法!
- C# 简单的从服务器下载文件代码
- C# 实现一个Log日志文件 以每2Mb创建一个新的日志
- C#实现的Excel文件操作类实例
- 用C#代码生成一个简单的PDF文件
- C#操作xml配置文件最简单方法
- C#简单的写日志方法
- C#拷贝文件简单实现方法
- c# log4net 日志输出到 本地文件 textbox listview DEMO
- C#开发之简单的日志打印类
- log日志文件的简单实现
- c#接简单数据库操作类
- C# 简单的日志系统(服务端)
- C# Excel ole文件读写操作类
- C#写入日志信息到文件中
- C#最简单写入读出XML配置文件