7 changed files with 669 additions and 270 deletions
Binary file not shown.
@ -0,0 +1,262 @@ |
|||
using System; |
|||
using System.Collections.Concurrent; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace WinFormsAppVisitorDeploy |
|||
{ |
|||
|
|||
|
|||
/// <summary>
|
|||
/// 日志记录帮助类
|
|||
/// </summary>
|
|||
public class MyLogHelper |
|||
{ |
|||
|
|||
/// <summary>
|
|||
/// 读取配置信息是否记录调试信息:1、不记录;其他、记录
|
|||
/// </summary>
|
|||
public static string MyLogDebug = System.Configuration.ConfigurationManager.AppSettings["MyLogDebug"] + ""; |
|||
|
|||
/// <summary>
|
|||
/// 正常消息队列
|
|||
/// </summary>
|
|||
private static ConcurrentQueue<LogInfoMo> infoMsgQueue = new ConcurrentQueue<LogInfoMo>(); |
|||
/// <summary>
|
|||
/// 调试消息队列
|
|||
/// </summary>
|
|||
private static ConcurrentQueue<LogInfoMo> debugMsgQueue = new ConcurrentQueue<LogInfoMo>(); |
|||
/// <summary>
|
|||
/// 错误消息队列
|
|||
/// </summary>
|
|||
private static ConcurrentQueue<LogInfoMo> errorMsgQueue = new ConcurrentQueue<LogInfoMo>(); |
|||
/// <summary>
|
|||
/// 控制文件写入线程
|
|||
/// </summary>
|
|||
private static Task logTask; |
|||
/// <summary>
|
|||
/// 记录信息
|
|||
/// </summary>
|
|||
/// <param name="infoObj"></param>
|
|||
public static void WriteMsg(LogInfoMo infoObj) |
|||
{ |
|||
try |
|||
{ |
|||
if (infoObj != null) |
|||
{ |
|||
switch (infoObj.msgType) |
|||
{ |
|||
case EnumLogMsgTypeEnum.Info: |
|||
infoMsgQueue.Enqueue(infoObj); |
|||
break; |
|||
case EnumLogMsgTypeEnum.Debug: |
|||
if (MyLogDebug == "1") |
|||
{ |
|||
//不记录调试信息
|
|||
} |
|||
else |
|||
{ |
|||
debugMsgQueue.Enqueue(infoObj); |
|||
} |
|||
break; |
|||
case EnumLogMsgTypeEnum.Error: |
|||
errorMsgQueue.Enqueue(infoObj); |
|||
break; |
|||
} |
|||
WriteExecute(); |
|||
} |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
infoObj.message = "队列异常:" + ex; |
|||
errorMsgQueue.Enqueue(infoObj); |
|||
} |
|||
} |
|||
/// <summary>
|
|||
/// 执行文件写入
|
|||
/// </summary>
|
|||
private static void WriteExecute() |
|||
{ |
|||
try |
|||
{ |
|||
//判断线程是否创建
|
|||
if (logTask == null) |
|||
{ |
|||
logTask = Task.Factory.StartNew(async () => |
|||
{ |
|||
while (true) |
|||
{ |
|||
//判断是否退出线程
|
|||
if (MyTaskControlHelper.TokenSource.IsCancellationRequested) |
|||
{ |
|||
return; |
|||
} |
|||
// 初始化为true时执行WaitOne不阻塞
|
|||
MyTaskControlHelper.ResetEvent.WaitOne(); |
|||
|
|||
// 模拟等待1ms
|
|||
await Task.Delay(1); |
|||
|
|||
//正常信息写入文件
|
|||
try |
|||
{ |
|||
if (infoMsgQueue.TryDequeue(out LogInfoMo infoObj)) |
|||
{ |
|||
WriteFire(infoObj); |
|||
} |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
string str = "普通信息队列处理异常:" + ex; |
|||
errorMsgQueue.Enqueue(new LogInfoMo() { message = str, msgType = EnumLogMsgTypeEnum.Error }); |
|||
} |
|||
|
|||
//错误信息写入文件
|
|||
try |
|||
{ |
|||
if (errorMsgQueue.TryDequeue(out LogInfoMo infoObj)) |
|||
{ |
|||
WriteFire(infoObj); |
|||
} |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
string str = "错误信息队列处理异常:" + ex; |
|||
errorMsgQueue.Enqueue(new LogInfoMo() { message = str, msgType = EnumLogMsgTypeEnum.Error }); |
|||
} |
|||
|
|||
//调试信息写入文件
|
|||
try |
|||
{ |
|||
if (debugMsgQueue.TryDequeue(out LogInfoMo infoObj)) |
|||
{ |
|||
WriteFire(infoObj); |
|||
} |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
string str = "调试信息队列处理异常:" + ex; |
|||
errorMsgQueue.Enqueue(new LogInfoMo() { message = str, msgType = EnumLogMsgTypeEnum.Error }); |
|||
} |
|||
|
|||
} |
|||
}); |
|||
} |
|||
|
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
string str = "文件队列处理异常:" + ex; |
|||
errorMsgQueue.Enqueue(new LogInfoMo() { message = str, msgType = EnumLogMsgTypeEnum.Error }); |
|||
} |
|||
} |
|||
/// <summary>
|
|||
/// 将信息写入文件
|
|||
/// </summary>
|
|||
/// <param name="msgType"></param>
|
|||
/// <param name="msg"></param>
|
|||
private static void WriteFire(LogInfoMo infoObj) |
|||
{ |
|||
try |
|||
{ |
|||
//获取程序所在目录
|
|||
string pathStr = AppDomain.CurrentDomain.BaseDirectory + "log\\" + (string.IsNullOrEmpty(infoObj.path) ? "" : infoObj.path + "\\") + |
|||
DateTime.Now.Year + "\\" + DateTime.Now.ToString("yyyyMM") + "\\" + DateTime.Now.ToString("yyyyMMdd") + "\\"; |
|||
//判断文件夹是否存在
|
|||
if (Directory.Exists(pathStr) == false) |
|||
{ |
|||
//不存在文件夹则创建
|
|||
Directory.CreateDirectory(pathStr); |
|||
} |
|||
string firePath = pathStr + infoObj.msgType.ToString() + DateTime.Now.ToString("yyyyMMddHH") + ".txt"; |
|||
|
|||
using (FileStream fs = new FileStream(firePath, FileMode.Append, FileAccess.Write)) |
|||
{ |
|||
using (StreamWriter sw = new StreamWriter(fs)) |
|||
{ |
|||
string str = DateTime.Now + "\t" + infoObj.message; |
|||
sw.WriteLine(str); |
|||
} |
|||
} |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
string str = DateTime.Now + "\t 文件写入异常:" + ex; |
|||
errorMsgQueue.Enqueue(new LogInfoMo() { message = str, msgType = EnumLogMsgTypeEnum.Error }); |
|||
} |
|||
} |
|||
|
|||
} |
|||
/// <summary>
|
|||
/// 日志信息类型枚举
|
|||
/// </summary>
|
|||
public enum EnumLogMsgTypeEnum |
|||
{ |
|||
/// <summary>
|
|||
/// 正常信息
|
|||
/// </summary>
|
|||
Info = 0, |
|||
/// <summary>
|
|||
/// 错误信息
|
|||
/// </summary>
|
|||
Error = 1, |
|||
/// <summary>
|
|||
/// 调试信息
|
|||
/// </summary>
|
|||
Debug = 2, |
|||
} |
|||
/// <summary>
|
|||
/// 操作日志记录类型
|
|||
/// </summary>
|
|||
public enum EnumOperationLogType |
|||
{ |
|||
//0、查询;1、新增;2、修改;3、删除;4、异常信息;5、其他
|
|||
|
|||
/// <summary>
|
|||
/// 查询
|
|||
/// </summary>
|
|||
Query = 0, |
|||
/// <summary>
|
|||
/// 新增
|
|||
/// </summary>
|
|||
Add = 1, |
|||
/// <summary>
|
|||
/// 更新
|
|||
/// </summary>
|
|||
Update = 2, |
|||
/// <summary>
|
|||
/// 删除
|
|||
/// </summary>
|
|||
Delete = 3, |
|||
/// <summary>
|
|||
/// 异常
|
|||
/// </summary>
|
|||
Error = 4, |
|||
/// <summary>
|
|||
/// 其他
|
|||
/// </summary>
|
|||
Other = 5, |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 记录信息数据模型
|
|||
/// </summary>
|
|||
public class LogInfoMo |
|||
{ |
|||
/// <summary>
|
|||
/// 信息保存路径,默认不单独保存
|
|||
/// </summary>
|
|||
public string path { get; set; } |
|||
/// <summary>
|
|||
/// 记录信息内容字符串
|
|||
/// </summary>
|
|||
public string message { get; set; } |
|||
/// <summary>
|
|||
/// 信息类型
|
|||
/// </summary>
|
|||
public EnumLogMsgTypeEnum msgType { get; set; } |
|||
} |
|||
} |
@ -0,0 +1,45 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace WinFormsAppVisitorDeploy |
|||
{ |
|||
/// <summary>
|
|||
/// Task线程统一控制帮助类
|
|||
/// </summary>
|
|||
public class MyTaskControlHelper |
|||
{ |
|||
/// <summary>
|
|||
/// 控制Task是否退出
|
|||
/// </summary>
|
|||
private static CancellationTokenSource tokenSource; |
|||
|
|||
/// <summary>
|
|||
/// 控制Task是否暂停
|
|||
/// </summary>
|
|||
private static ManualResetEvent resetEvent = new ManualResetEvent(true); |
|||
|
|||
/// <summary>
|
|||
/// 获取Task退出控制器
|
|||
/// </summary>
|
|||
public static CancellationTokenSource TokenSource |
|||
{ |
|||
get |
|||
{ |
|||
if (tokenSource == null) |
|||
{ |
|||
tokenSource = new CancellationTokenSource(); |
|||
} |
|||
return tokenSource; |
|||
} |
|||
} |
|||
/// <summary>
|
|||
/// 获取Task暂停控制器
|
|||
/// </summary>
|
|||
public static ManualResetEvent ResetEvent { get { return resetEvent; } } |
|||
|
|||
} |
|||
} |
Loading…
Reference in new issue