简单的文件读写来写log,功能不多但能满足日常使用,资源占用小,上代码:
示例程序百度云链接:
链接:https://pan.baidu.com/s/15bc2Q52Jwzysv6_xnWD41w
提取码:afms
复制这段内容后打开百度网盘手机App,操作更方便哦
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
/// <summary>
/// 日志记录工具
/// </summary>
public static class Log
{
private static readonly Thread LogThread;
private static readonly ConcurrentQueue<string> LogQueue; //自定义线程安全的Queue
private static readonly object SyncRoot;
private static readonly string FilePath;
private static bool IsRun;
public static bool IsWriteLog;//可以设置是否启用日志,一般只需要前期写大量的日志方便排查bug,后期程序稳定了则不需要了.异常日志建议都开启
/// <summary>
/// 日志类型枚举
/// </summary>
public enum LogType
{
/// <summary>
/// 一般输出
/// </summary>
Trace,
/// <summary>
/// 警告
/// </summary>
Warning,
/// <summary>
/// 错误
/// </summary>
Error,
/// <summary>
/// SQL语句
/// </summary>
SQL
}
/// <summary>
/// 因为线程是死循环读取队列,在没有日志数据的时候可能会消耗不必要的资源,所有当队列没有数据的时候用该类控制线程的(继续和暂停)
/// </summary>
private static readonly AutoResetEvent AutoReset = null;
static Log()
{
AutoReset = new AutoResetEvent(false);
SyncRoot = new object();
FilePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "运行日志\\";
LogQueue = new ConcurrentQueue<string>();
IsRun = true;
IsWriteLog = true;
LogThread = new Thread(WriteLog);
LogThread.IsBackground = true;
LogThread.Start();
}
//LogThread.IsBackground =false时,关闭程序时需要主动释放
public static void Dispose()
{
try
{
if (IsRun)
{
IsRun = false;
AutoReset.Set();
AutoReset.Dispose();
}
}
catch (Exception)
{
throw;
}
}
public static void log(string msg)
{
try
{
if (!IsWriteLog) return;
string _msg = string.Format("{0} : {1}", DateTime.Now.ToString("HH:mm:ss:fff"), msg);
LogQueue.Enqueue(_msg);
if (AutoReset != null)
{
AutoReset.Set();
}
}
catch (Exception)
{
throw;
}
}
public static void MemoryInfo()
{
try
{
if (!IsWriteLog) return;
string msg;
Process CurrentProcess = Process.GetCurrentProcess();
//获取当前进程占用内存:
long memory = CurrentProcess.WorkingSet64 / 1048576;
msg = "当前进程占用内存:" + memory.ToString() + "MB;";
string _msg = string.Format("{0} : {1}", DateTime.Now.ToString("HH:mm:ss:fff"), msg);
LogQueue.Enqueue(_msg);
AutoReset.Set();
}
catch (Exception)
{
throw;
}
}
public static void log(string msg, LogType type)
{
try
{
if (!IsWriteLog) return;
string _msg = string.Format("{0} {1}: {2}", DateTime.Now.ToString("HH:mm:ss:fff"), type, msg);
LogQueue.Enqueue(_msg);
AutoReset.Set();
}
catch (Exception)
{
throw;
}
}
public static void log(Exception ex)
{
try
{
if (ex != null)
{
string _newLine = string.Empty;
StringBuilder _builder = new StringBuilder();
_builder.AppendFormat("{0}: {1}{2}", DateTime.Now.ToString("HH:mm:ss:fff"), ex.Message, _newLine);
_builder.AppendFormat("{0}{1}", ex.GetType(), _newLine);
_builder.AppendFormat("{0}{1}", ex.Source, _newLine);
_builder.AppendFormat("{0}{1}", ex.TargetSite, _newLine);
_builder.AppendFormat("{0}{1}", ex.StackTrace, _newLine);
LogQueue.Enqueue(_builder.ToString());
AutoReset.Set();
}
}
catch (Exception)
{
throw;
}
}
private static void WriteLog()
{
try
{
while (IsRun)
{
if (LogQueue.Count() > 0)
{
string _msg;
LogQueue.TryDequeue(out _msg);
if (!string.IsNullOrWhiteSpace(_msg))
{
Monitor.Enter(SyncRoot);
if (!CreateDirectory()) continue;
string _path = string.Format("{0}{1}.log", FilePath, DateTime.Now.ToString("yyyy-MM-dd"));
Monitor.Exit(SyncRoot);
lock (SyncRoot)
{
if (CreateFile(_path))
ProcessWriteLog(_path, _msg); //写入日志到文本
}
}
}
else
{
AutoReset.WaitOne();
}
}
}
catch (Exception)
{
throw;
}
}
private static void ProcessWriteLog(string path, string msg)
{
try
{
StreamWriter _sw = File.AppendText(path);
_sw.WriteLine(msg);
_sw.Flush();
_sw.Close();
}
catch (Exception ex)
{
Debug.WriteLine(string.Format("写入日志失败,原因:{0}", ex.Message));
}
}
private static bool CreateFile(string path)
{
bool _result = true;
try
{
if (!File.Exists(path))
{
FileStream _files = File.Create(path);
_files.Close();
}
}
catch (Exception)
{
_result = false;
}
return _result;
}
private static bool CreateDirectory()
{
bool _result = true;
try
{
if (!Directory.Exists(FilePath))
{
Directory.CreateDirectory(FilePath);
}
}
catch (Exception)
{
_result = false;
}
return _result;
}
}
正则忽略大小写 – RegexOptions.IgnoreCase 例如: 复制代码 代码如下: Str = R...
DELETEFROMTablesWHEREIDNOTIN(SELECTMin(ID)FROMTablesGROUPBYName) Min的话保...
4月11日20:30~22:00通过腾讯会议进行了第二次在线学习讨论我把学习笔记整理一下...
错误描述: 在开发.net项目中,通过microsoft.ACE.oledb读取excel文件信息时,报...
上篇文章给大家介绍了 Java正则表达式匹配,替换,查找,切割的方法 ,接下来,...
项目中用到的一些特殊字符和图标 html代码 XML/HTML Code 复制内容到剪贴板 div ...
Elasticsearch 是通过 Lucene 的倒排索引技术实现比关系型数据库更快的过滤。特...
复制代码 代码如下: % URL="http://news.163.com/special/00011K6L/rss_newstop....
工具:Eclipse,Oracle,smartupload.jar;语言:jsp,Java;数据存储:Oracle。...
本文实例讲述了Laravel框架源码解析之反射的使用。分享给大家供大家参考,具体如...