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.
76 lines
2.4 KiB
76 lines
2.4 KiB
using System; |
|
using System.Collections.Generic; |
|
using System.Configuration; |
|
using System.IO; |
|
using System.Linq; |
|
using System.Text; |
|
using System.Threading.Tasks; |
|
|
|
namespace test |
|
{ |
|
/// <summary> |
|
/// 自定义日志 |
|
/// </summary> |
|
public class LogHelpe |
|
{ |
|
/// <summary> |
|
/// 信息日志 |
|
/// </summary> |
|
/// <param name="str"></param> |
|
public static void WriteInfoLog(string str, string dirName = @"logs") |
|
{ |
|
//return; |
|
try |
|
{ |
|
var status = 1;//int.Parse(ConfigurationManager.AppSettings["LogStatus"]); |
|
if (status == 0) // 0不输出日志,1输出日志 |
|
{ |
|
return; |
|
} |
|
else |
|
{ |
|
if (string.IsNullOrEmpty(str)) return; |
|
|
|
var baseDir = AppDomain.CurrentDomain.BaseDirectory + dirName; |
|
if (!Directory.Exists(baseDir)) |
|
{ |
|
Directory.CreateDirectory(baseDir); |
|
} |
|
string filePath = Path.Combine(baseDir, DateTime.Now.ToString("yyyy-MM-dd") + "_infolog.txt"); |
|
using (StreamWriter sw = File.AppendText(filePath)) |
|
{ |
|
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff ") + "[" + str + "]" + "\r\n"); |
|
sw.Flush(); |
|
} |
|
} |
|
} |
|
catch { } |
|
} |
|
|
|
/// <summary> |
|
/// 错误日志 |
|
/// </summary> |
|
/// <param name="str"></param> |
|
public static void WriteErrorLog(string str, string dirName = @"logs") |
|
{ |
|
//return; |
|
try |
|
{ |
|
if (string.IsNullOrEmpty(str)) return; |
|
|
|
var baseDir = AppDomain.CurrentDomain.BaseDirectory + dirName; |
|
if (!Directory.Exists(baseDir)) |
|
{ |
|
Directory.CreateDirectory(baseDir); |
|
} |
|
string filePath = Path.Combine(baseDir, DateTime.Now.ToString("yyyy-MM-dd") + "_errorlog.txt"); |
|
using (StreamWriter sw = File.AppendText(filePath)) |
|
{ |
|
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff ") + "[" + str + "]" + "\r\n"); |
|
sw.Flush(); |
|
} |
|
} |
|
catch { } |
|
} |
|
} |
|
}
|
|
|