c# 日志满2M自动新建日志文件
2018-01-29 10:59
281 查看
向系统记录日志时,遇到日志文件过大时,对于读取和写入都会耗费时间和内存;
考虑此情况,使用日志分多文件写入的方式日志文件满2M自动新建日志文件,原日志另命名存储日志。
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/********************************************************************
** File Name: 日志类
** Author: Luc
** Create Time: 2018-01-18 15:20
** Modifier:
** Modify Time:
** Description: 记录日志信息
********************************************************************/
namespace test.BLL
{
public class WritLog
{
/// <summary>
/// 写日志信息(每满2M新建日志)luc:2017-1-17
/// </summary>
/// <param name="message">错误信息内容</param>
/// <param name="From">发生错误的来源(命名空间+类名+方法名),可为空</param>
public static void Writelog(string message)
{
string logpath = ConfigurationManager.AppSettings["LogPath"];
CommonLog(message, logpath);
}
/// <summary>
/// windows服务日志
/// </summary>
/// <param name="message">日志信息</param>
/// <param name="servername">服务名</param>
public static void WriteServerLog(string message, string servername)
{
string logpath = ConfigurationManager.AppSettings["LogServerath"];
string fullpath = GetPathStr(logpath, string.Format("{0}.log", servername));
CommonLog(message, fullpath);
}
public static void CommonLog(string message, string fullpath)
{
//文件所在路径
string logpath = Path.GetDirectoryName(fullpath);
//文件名称
string filename = Path.GetFileNameWithoutExtension(fullpath);
if (!Directory.Exists(logpath))
{
Directory.CreateDirectory(logpath);
}
if (!File.Exists(fullpath))
{
using (File.Create(fullpath)) { }
}
FileInfo fileinfo = new FileInfo(fullpath);
//获取指定目录下的所有的子文件
string[] files = Directory.GetFiles(logpath, filename + "*", SearchOption.TopDirectoryOnly);
if (fileinfo.Length > 2 * 1024 * 1024)
{
File.Move(fullpath, GetPathStr(logpath, string.Format("{0}.log", filename + "Old" + files.Length)));
if (!File.Exists(fullpath))
{
using (File.Create(fullpath)) { }
}
}
using (StreamWriter sw = File.AppendText(fullpath))
{
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":" + "\t\n");
sw.WriteLine("-----------------------" + "\t\n");
sw.WriteLine("Message :" + message + "\t\n");
sw.WriteLine("====================================================================" + "\t\n\t\n\t\n");
sw.Close();
}
}
/// <summary>
/// 拼接地址串
/// </summary>
/// <param name="firstPath"></param>
/// <param name="secondPath"></param>
/// <returns></returns>
private static string GetPathStr(string firstPath, string secondPath)
{
StringBuilder builder = new StringBuilder();
builder.Append(firstPath);
builder.Append("\\");
builder.Append(secondPath);
return builder.ToString();
}
}
}
考虑此情况,使用日志分多文件写入的方式日志文件满2M自动新建日志文件,原日志另命名存储日志。
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/********************************************************************
** File Name: 日志类
** Author: Luc
** Create Time: 2018-01-18 15:20
** Modifier:
** Modify Time:
** Description: 记录日志信息
********************************************************************/
namespace test.BLL
{
public class WritLog
{
/// <summary>
/// 写日志信息(每满2M新建日志)luc:2017-1-17
/// </summary>
/// <param name="message">错误信息内容</param>
/// <param name="From">发生错误的来源(命名空间+类名+方法名),可为空</param>
public static void Writelog(string message)
{
string logpath = ConfigurationManager.AppSettings["LogPath"];
CommonLog(message, logpath);
}
/// <summary>
/// windows服务日志
/// </summary>
/// <param name="message">日志信息</param>
/// <param name="servername">服务名</param>
public static void WriteServerLog(string message, string servername)
{
string logpath = ConfigurationManager.AppSettings["LogServerath"];
string fullpath = GetPathStr(logpath, string.Format("{0}.log", servername));
CommonLog(message, fullpath);
}
public static void CommonLog(string message, string fullpath)
{
//文件所在路径
string logpath = Path.GetDirectoryName(fullpath);
//文件名称
string filename = Path.GetFileNameWithoutExtension(fullpath);
if (!Directory.Exists(logpath))
{
Directory.CreateDirectory(logpath);
}
if (!File.Exists(fullpath))
{
using (File.Create(fullpath)) { }
}
FileInfo fileinfo = new FileInfo(fullpath);
//获取指定目录下的所有的子文件
string[] files = Directory.GetFiles(logpath, filename + "*", SearchOption.TopDirectoryOnly);
if (fileinfo.Length > 2 * 1024 * 1024)
{
File.Move(fullpath, GetPathStr(logpath, string.Format("{0}.log", filename + "Old" + files.Length)));
if (!File.Exists(fullpath))
{
using (File.Create(fullpath)) { }
}
}
using (StreamWriter sw = File.AppendText(fullpath))
{
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":" + "\t\n");
sw.WriteLine("-----------------------" + "\t\n");
sw.WriteLine("Message :" + message + "\t\n");
sw.WriteLine("====================================================================" + "\t\n\t\n\t\n");
sw.Close();
}
}
/// <summary>
/// 拼接地址串
/// </summary>
/// <param name="firstPath"></param>
/// <param name="secondPath"></param>
/// <returns></returns>
private static string GetPathStr(string firstPath, string secondPath)
{
StringBuilder builder = new StringBuilder();
builder.Append(firstPath);
builder.Append("\\");
builder.Append(secondPath);
return builder.ToString();
}
}
}
相关文章推荐
- C# 自动查找文件内容(正则使用、获取目录下所有文件、多线程、日志记录,文件操作)
- C# 自动备份和清理日志文件
- Android新建项目后src下没有自动生成文件
- C# 目录操作(新建,删除),获取目录下文件列表
- eclipse新建java文件时自动生成注释
- win 系统设置weblogic 进行定时自动重启并删除其日志和缓存文件,定时监控cpu是否达到100%并重启weblogic服务
- C#实现Log4Net日志分类和自动维护实例
- C#中使用Log4net日志输出到本地文件、Textbox或Listview
- 使用yyi2 框架写日志,日志文件自动分多个文件问题处理
- AndroidStudio 新建文件自动添加到svn版本控制
- 打印log4j日志 : 超过一定大小新建日志文件 & 每天新建一个日志文件
- C# 创建文件时,文件夹不存在,如何自动创建文件夹
- iOS 开发 Pch 文件的正确使用1.存放一些全局的宏(整个项目中都用得上的宏) 2.用来包含一些全部的头文件(整个项目中都用得上的头文件) 3.能自动打开或者关闭日志输出功能
- editplus配置笔记(新建文件模板、代码美化、PHP手册、函数自动完成)
- C#生成安装文件后自动附加数据库的思路跟算法
- 一个可创建读取日志的管理类(可固定创建2M大小的日志文件)
- 自动更改Windows新建用户桌面文件默认路径修改
- 使用rsyslog限制日志文件大小,outchannel实现自动回滚rotate
- Office文件上传自动生成缩略图-C#开发
- c#自动附加分离备份还原数据库 C#生成安装文件后自动附加数据库的思路跟算法 打包之后,运行之前