using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Interface
{
public class Log4Test
{
//准备定义一个全局变量,所有的地方共享,但是发现在多任务里面还是应该自己传递过来
//public readonly static string logPath = @"D:\十大股东部署文件\文件解析服务\Config\";
/// <summary>
/// 用来写日志的公用方法
/// </summary>
/// <param name="msg">日志的内容</param>
/// <param name="logPath">定义存储日志的路径</param>
/// <param name="type">日志类型;0</param>
public static void WriteLog(string msg, string logPath, string type)
{
if (!Directory.Exists(logPath))
Directory.CreateDirectory(logPath);
type = type ?? "0";
logPath = logPath ?? Environment.CurrentDirectory;
try
{
using (StreamWriter sw = File.AppendText(string.Format("{0}{1:yyyy-MM-dd}.txt", logPath, DateTime.Now)))
{
if (type == "0")
sw.WriteLine("************************异常开始**************************");
sw.WriteLine("时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
sw.WriteLine("消息:" + msg);
if (type == "0")
sw.WriteLine("************************异常结束**************************");
sw.WriteLine();
sw.Flush();
sw.Close();
sw.Dispose();
}
}
catch (IOException e)
{
using (StreamWriter sw = File.AppendText(string.Format("{0}{1:yyyy-MM-dd}.txt", logPath, DateTime.Now)))
{
if (type == "0")
sw.WriteLine("************************异常开始**************************");
sw.WriteLine("时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
if (type == "0")
sw.WriteLine("消息:" + e.Message);
sw.WriteLine("************************异常结束**************************");
sw.WriteLine();
sw.Flush();
sw.Close();
sw.Dispose();
}
}
}
}
}
项目之写日志的相关方法
最新推荐文章于 2022-02-15 09:52:01 发布