You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
2.6 KiB
93 lines
2.6 KiB
9 months ago
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.IO;
|
||
|
using System.Linq;
|
||
|
using System.Text;
|
||
|
using System.Threading.Tasks;
|
||
|
|
||
|
namespace CameraErrorCheck
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 文件帮助类
|
||
|
/// </summary>
|
||
|
public class MyFileLogHelper
|
||
|
{
|
||
|
|
||
|
/// <summary>
|
||
|
/// 将信息写入文件
|
||
|
/// </summary>
|
||
|
/// <param name="msgType"></param>
|
||
|
/// <param name="msg"></param>
|
||
|
public 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;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
/// <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; }
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 日志信息类型枚举
|
||
|
/// </summary>
|
||
|
public enum EnumLogMsgTypeEnum
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 正常信息
|
||
|
/// </summary>
|
||
|
Info = 0,
|
||
|
/// <summary>
|
||
|
/// 错误信息
|
||
|
/// </summary>
|
||
|
Error = 1,
|
||
|
/// <summary>
|
||
|
/// 调试信息
|
||
|
/// </summary>
|
||
|
Debug = 2,
|
||
|
}
|
||
|
}
|