110 lines
3.3 KiB
C#
110 lines
3.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Web;
|
|
|
|
namespace HZQR.Common
|
|
{
|
|
/// <summary>
|
|
/// 文本日志帮助类
|
|
/// </summary>
|
|
public class LogUtil
|
|
{
|
|
/// <summary>
|
|
/// 记录文本日志
|
|
/// </summary>
|
|
/// <param name="ex">异常</param>
|
|
public static void WriteLog(Exception ex)
|
|
{
|
|
WriteLog(ex, "");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 记录文本日志
|
|
/// </summary>
|
|
/// <param name="msg">日志内容</param>
|
|
public static void WriteLog(string msg)
|
|
{
|
|
WriteLog(null, msg);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 记录文本日志
|
|
/// </summary>
|
|
/// <param name="ex">异常</param>
|
|
/// <param name="otherMsg">日志内容</param>
|
|
public static void WriteLog(Exception ex, string otherMsg)
|
|
{
|
|
WriteLog(ex, otherMsg, "");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 记录文本日志
|
|
/// </summary>
|
|
/// <param name="ex"></param>
|
|
/// <param name="otherMsg"></param>
|
|
/// <param name="FileName"></param>
|
|
public static void WriteLog(Exception ex, string otherMsg, string FileName)
|
|
{
|
|
WriteLog(ex, otherMsg, FileName, "");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 记录文本日志
|
|
/// </summary>
|
|
/// <param name="ex">异常</param>
|
|
/// <param name="otherMsg">日志内容</param>
|
|
/// <param name="FileName">日志文件名称</param>
|
|
/// <param name="DirectoryName">文件存储路径</param>
|
|
public static void WriteLog(Exception ex, string otherMsg, string FileName, string DirectoryName)
|
|
{
|
|
string str = "";
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(FileName))
|
|
{
|
|
FileName = DateTime.Now.ToString("yyyyMMdd");
|
|
}
|
|
if (string.IsNullOrWhiteSpace(DirectoryName))
|
|
{
|
|
DirectoryName = AppDomain.CurrentDomain.BaseDirectory + "log";
|
|
}
|
|
str += string.Format(@"{0}", DateTime.Now.ToString());
|
|
|
|
if (ex != null)
|
|
{
|
|
str += string.Format(@"
|
|
Message:{0}
|
|
StackTrace:
|
|
{1}
|
|
Source:{2}
|
|
"
|
|
, ex.Message
|
|
, ex.StackTrace
|
|
, ex.Source
|
|
|
|
);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(otherMsg))
|
|
{
|
|
str += string.Format("{0}", otherMsg);
|
|
}
|
|
|
|
if (!Directory.Exists(DirectoryName))//判断文件夹是否存在
|
|
{
|
|
Directory.CreateDirectory(DirectoryName);//不存在则创建文件夹
|
|
}
|
|
string filePath = DirectoryName + "\\" + FileName + ".txt";
|
|
StreamWriter sw = new StreamWriter(filePath, true);
|
|
sw.WriteLine(str);
|
|
sw.Close();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
WriteLog(exception, "", "", @"D:\RebuildLog");
|
|
}
|
|
}
|
|
}
|
|
} |