public static void WriteLog(string info, string str)
{
string logDateDirPath = @"./Log/" + DateTime.Now.ToString("yyyyMMdd");
string logFileName = logDateDirPath + "\\" + str + ".log"; ;
if (!Directory.Exists(logDateDirPath))
{
Directory.CreateDirectory(logDateDirPath);
}
try
{
TextWriter tw = new StreamWriter(logFileName, true, System.Text.Encoding.Default); ;
tw.WriteLine(info);
tw.Close();
}
catch (Exception ex)
{
}
}
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
int i = 0;
try
{
int j = 10 / i;
}
catch (Exception ex)
{
OutputErrFile(ex.StackTrace.ToString());
}
}
static private void OutputErrFile(string strContent)
{
string fileContent = "";
string logDateDirPath = @"./errLog/";
string strFilePath = logDateDirPath + DateTime.Now.ToString("yyyyMMdd") + @" ErrLog.txt";
if (!Directory.Exists(logDateDirPath))
{
Directory.CreateDirectory(logDateDirPath);
}
if (!ContentIsContain(strFilePath,strContent))
{
using (StreamWriter swOut = new StreamWriter(strFilePath, true, Encoding.Default))
{
swOut.WriteLine(strContent);
swOut.WriteLine("时间:" + DateTime.Now);
string str = "-";
for (int i = 0; i < 10; i++)
{
str += str;
}
swOut.WriteLine(str);
swOut.Flush();
}
}
}
static bool ContentIsContain(string strFilePath, string goalContent)
{
string fileContent = "";
if (File.Exists(strFilePath))
{
using (StreamReader sr = new StreamReader(strFilePath))
{
fileContent = sr.ReadToEnd();
}
}
if (fileContent.Contains(goalContent))
{
return true;
}
return false;
}
}
}