LogInfoHelper
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading; namespace Model
{
/// <summary>
/// 日志文件帮助类
/// </summary>
public class LogInfoHelper
{
#region Field
private static Object m_Lock = new object();
// 消息队列 (内部加锁访问)
private Queue<IGetWriteLogInfo> m_MessageQueue = new Queue<IGetWriteLogInfo>();
private Thread m_BackWriteThread;
private static LogInfoHelper m_Instance = null;
private static readonly object syncObj = new object();
#endregion #region Method
/// <summary>
/// 向日志文件中写入运行时信息
/// </summary>
/// <param name="message">要写入的运行时消息</param>
/// <param name="messageType"></param>
/// <returns></returns>
private bool WriteRunTimeMessage(IGetWriteLogInfo logInfo)
{
lock (m_Lock)
{
m_MessageQueue.Enqueue(logInfo);
}
return true;
} /// <summary>
/// 向日志文件中写入运行时的程序信息
/// </summary>
/// <param name="Message">要写入到日志文件中的信息</param>
/// <param name="messageType">要写入的日志类型</param>
/// <param name="isAutoAddTimeInfo">是否在写入的信息前加上当前系统时间</param>
/// <returns></returns>
public bool WriteRunTimeMessage(IGetWriteLogInfo logInfo, bool isAutoAddTimeInfo)
{
logInfo.Message = DateTime.Now.ToString() + "->" + logInfo.Message;
return WriteRunTimeMessage(logInfo);
}
/// <summary>
/// 异步写文件线程入口函数
/// </summary>
private void WriteTreadHandler()
{
Queue<IGetWriteLogInfo> tempQueue = new Queue<IGetWriteLogInfo>();
while (true)
{
StreamWriter sw = null;
#region 加锁拷贝部分
lock (m_MessageQueue)
{
int nowMessageCount = m_MessageQueue.Count;
for (int i = 0; i < nowMessageCount; i++)
{
try
{
tempQueue.Enqueue(m_MessageQueue.Dequeue());
}
catch (System.InvalidOperationException exp)
{
break;
}
}
}
#endregion
try
{
int nowTempQueueCount = tempQueue.Count;
for (int i = 0; i < nowTempQueueCount; i++)
{
IGetWriteLogInfo messageInfo = tempQueue.Dequeue();
long fileSize = 0; DateTime dtNow = DateTime.Now;
//日志文件名称
string FileName = messageInfo.BaseFileName + "-" + dtNow.ToShortDateString() + ".log";
//日志文件的完整路径
string FilePath = System.IO.Path.Combine(messageInfo.DictionaryPath, FileName);
FilePath = FilePath.Replace("/", "-");
//判断日志文件的大小
if (File.Exists(FilePath))
{
FileInfo FileInfo = new FileInfo(FilePath);
fileSize = FileInfo.Length / 1024 / 1024;
//当天异常日志大于10M的时候停止继续写入异常信息(PS:万一异常超级多 硬盘岂不是要炸~_~ 服务器上全天跑着鬼知道那儿有bug)
if (fileSize > 10)
{
continue;
}
}
sw = new StreamWriter(FilePath, true, Encoding.UTF8, 1024);
sw.AutoFlush = true;
sw.WriteLine(messageInfo.Message);
sw.Close();
//sw.WriteLine("------------------------------------------------------------");
}
}
catch (Exception exp)
{
throw exp;
}
finally
{
if (sw != null)
{
sw.Close();
sw.Dispose();
}
}
Thread.Sleep(1000);
}
}
#endregion #region Constructor
/// <summary>
/// 实例化对象的时候初始化写入线程
/// </summary>
private LogInfoHelper()
{
m_BackWriteThread = new Thread(WriteTreadHandler);
m_BackWriteThread.IsBackground = true;
m_BackWriteThread.Name = "异步写入日志文件线程";
m_BackWriteThread.Start();
}
#endregion
/// <summary>
/// 获取LogInfoHelper的实例
/// </summary>
public static LogInfoHelper GetInstance()
{
if (null == m_Instance)
{
lock (syncObj)
{
if (null == m_Instance)
{
m_Instance = new LogInfoHelper();
}
}
}
return m_Instance;
} } /// <summary>
/// 运行时需要写入到日志文件的信息类型
/// </summary>
public class LogMessageInfo : IGetWriteLogInfo
{ #region field
private string m_BaseName; private string m_MessageType; private string m_Message = string.Empty; private string m_DictionaryPath = string.Empty;
#endregion #region property
/// <summary>
/// 写入日志文件的基础文件名称
/// </summary>
public string BaseFileName
{
get { return m_BaseName; }
}
/// <summary>
/// 写入日志文件的日志类型
/// </summary>
public string MessageType
{
get { return m_MessageType; }
}
/// <summary>
/// 写入日志文件的日志信息
/// </summary>
public string Message
{
get { return m_Message; }
set { if (!string.IsNullOrEmpty(value)) { m_Message = value; } }
}
/// <summary>
/// 写入日志的文件存放目录
/// </summary>
public string DictionaryPath
{
get { return m_DictionaryPath; }
}
#endregion #region constructor
public LogMessageInfo(string DictionaryPath, string MessageFileBaseString, string MessageType, string Message)
{
if (!string.IsNullOrEmpty(DictionaryPath) && System.IO.Directory.Exists(DictionaryPath))
{
m_DictionaryPath = DictionaryPath;
}
else
{
throw new Exception("指定存放异常文件的目录不存在或者错误!!");
}
if (!string.IsNullOrEmpty(MessageFileBaseString) && !string.IsNullOrEmpty(MessageType))
{
m_BaseName = MessageFileBaseString;
m_MessageType = MessageType;
m_Message = Message;
}
}
#endregion /// <summary>
/// 获取要写入到日志文件的字符串数据
/// </summary>
/// <returns></returns>
public string GetLogString()
{
return m_Message;
}
} /// <summary>
/// 获取要写入到日志文件中的文字字符串
/// </summary>
public interface IGetWriteLogInfo
{
/// <summary>
/// 获取要写入到日志文件的字符串信息
/// </summary>
/// <returns></returns>
string GetLogString();
/// <summary>
/// 获取写入的日志文件的基名称
/// </summary>
string BaseFileName { get; }
/// <summary>
/// 获取写入的日志的日志类型
/// </summary>
string MessageType { get; }
/// <summary>
/// 获取要写入的日志的消息
/// </summary>
string Message { get; set; }
/// <summary>
/// 获取要写入日志文件的文件夹路径
/// </summary>
string DictionaryPath { get; }
}
}
LogInfoHelper的更多相关文章
- Pagination.js + Sqlite web系统分页
前端使用 jquery pagination.js 插件. 环境准备:jquery.js.pagination.js.pagination.css 附件下载:https://files.cnblogs ...
随机推荐
- HDU - 1712 (分组背包模板)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1712 题意:给你n个课程,每个课程有很多种学习方法,用的时间和取得的效果都不一样,现在你只有m天时间用来学 ...
- postgresql数字类型
postgresql的数据类型很多,也可以使用create type命令创建自定义数据类型,但常用的数据类型是以下三种: l 数字数据类型 l 字符串数据类型 l 日期/时间数据类型 数字数据类 ...
- HDU 6659 Acesrc and Good Numbers (数学 思维)
2019 杭电多校 8 1003 题目链接:HDU 6659 比赛链接:2019 Multi-University Training Contest 8 Problem Description Ace ...
- 剑指offer——72圆圈中最后剩下的数字
题目描述 每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此.HF作为牛客的资深元老,自然也准备了一些小游戏.其中,有个游戏是这样的:首先,让小朋友们围成一个大圈.然后,他随机指 ...
- CSS Sprite初探之原理、使用
CSS Sprite简介: 利用CSS Sprites能很好地减少了网页的http请求次数,从而大大的提高了页面的性能,节省时间和带宽.CSS Sprites在国内很多人叫css精灵, 是一种网页图片 ...
- IDEA下将dubbo简单项目跑Demo(2019.12版)
项目架构(聚合项目,父子模块) src没用,所以删去 选择maven项目,不用勾选模板骨架,直接main方法,因为不用到服务器 顺序是按照:添加pom依赖-接口实现类-配置文件 项目环境 IDE:In ...
- 基于角色访问控制的OA系统的设计与实现
摘要:随着电子政务的快速发展和全面普及,办公自动化(OA)系统的安全性显得越来越重要.对基于Web 的B/S 结构的OA 系统结构和安全需求进行了分析,为了增强用户身份鉴别和授权控制的安全性,分析了基 ...
- vue 在微信中设置动态标题
1.安装插件 cnpm install vue-wechat-title --save 2.在main.js中引入 import VueWechatTitle from 'vue-wechat-tit ...
- 【颓废篇】Py:从零开始的poj自动提交
之前学习了一些python的爬虫技术... 已经可以通过python来水blog的阅读量了 你知道的太多了, 然而你看我这个blog惨不忍睹的访问量, 有人吗? 有人吗? 今天突然又双叒叕心血来潮想写 ...
- Tools: geos 使用指南
1. 下载geos 2. 进入VS开发人员命令提示3.依次执行如下命令 >VCVARS32.BAT>cd D:\DevTool\geos-3.7.0>atuogen.bat>n ...