using System;
using System.Text;
using System.IO;
using System.Diagnostics;
namespace Pcb.Common
{
///
///
///
public class LogHelper
{
private string _FilePath = "";
public static object LogLock = new object();
public LogHelper()
{
CreateDirectory();
}
///
/// 错误日志
///
///
public static void Error(string message)
{
(new LogHelper()).WtireLog("Error", message);
}
///
/// 错误日志
///
///
public static void Error(string message, Exception ex)
{
(new LogHelper()).WtireLog("Error", message + ":(行:" + ex.StackTrace + ")" + ex.ObjectToString());
}
///
/// 错误日志
///
///
public static void Error(Exception ex)
{
(new LogHelper()).WtireLog("Error", ex.ToString());
}
// ///
///// 错误日志
/////
/////
//public static void BlockInfo(string message, string message2)
//{
// (new LogHelper()).WtireLog("BlockInfo", message + ":" + message2);
//}
///
/// 警告日志
///
///
public static void Warn(string message)
{
(new LogHelper()).WtireLog("Warn", message);
}
///
/// 信息日志
///
///
public static void Info(string message)
{
(new LogHelper()).WtireLog("Info", message);
}
///
/// 信息日志
///
///
public static void Info(string type, string message)
{
(new LogHelper()).WtireLog(type, message);
}
///
/// create directory if filePathSection has string
///
private void CreateDirectory()
{
try
{
string root = GetLogFileRootPath();
if (!System.IO.Directory.Exists(root))
{
System.IO.Directory.CreateDirectory(root);
}
this.RemoveDirectoryInfoReadOnly(root);
_FilePath = root;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
private void WtireLog(string type, string message, string title = "")
{
_FilePath += "\\Log" + type;
if (!Directory.Exists(_FilePath))
{
// LogHelper.Info(_FilePath);
Directory.CreateDirectory(_FilePath);
}
string msg = "
异常时间:" + System.DateTime.Now.ToString("HH:mm:ss fff") + "
异常级别:" + type + "
" + message;
try
{
lock (LogHelper.LogLock)
{
string path = string.Empty;
path = _FilePath + "\\" + System.DateTime.Now.ToString("yyyyMMdd") + ".html";
FileInfo finfo = new FileInfo(path);
if (!finfo.Exists)
{
StreamWriter sw = new StreamWriter(path, false, Encoding.GetEncoding("UTF-8"));
sw.Write("");
sw.Close();
}
using (StreamWriter fs = finfo.AppendText())
{
StreamWriter w = fs;//new StreamWriter(fs);
w.BaseStream.Seek(0, SeekOrigin.End);
w.WriteLine(msg);
w.Flush();
w.Close();
}
var splitPath = _FilePath + "\\" + System.DateTime.Now.ToString("yyyyMMdd_HHmmssfff") + "_{0}.html";
var m1 = 1024 * 1024;
FileCommonHelper.SplitFile(path, m1, m1, splitPath);
}
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.ToString());
}
}
///
/// log file root path
///
private static string GetLogFileRootPath()
{
string logPath = "";// (string)System.Configuration.ConfigurationManager.AppSettings["TextLog.Path"];
try
{
if (logPath.Length >= 2)
{
if (logPath.Substring(0, 2) == @"\\")
{
return logPath;
}
else if (logPath.Substring(1, 1) == ":")
{
return logPath;
}
else
{
return System.AppDomain.CurrentDomain.BaseDirectory + "Log\\" + logPath;
}
}
else
{
return System.AppDomain.CurrentDomain.BaseDirectory + "Log\\" + logPath;
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
return System.AppDomain.CurrentDomain.BaseDirectory;
}
}
///
/// remove directory readonly property
///
///
private void RemoveDirectoryInfoReadOnly(string filepath)
{
try
{
if (File.Exists(filepath))
{
System.IO.DirectoryInfo finfo = new DirectoryInfo(filepath);
if (finfo.Attributes.ToString().IndexOf("ReadOnly") != -1)
finfo.Attributes = FileAttributes.Normal;
}
}
catch { }
}
public static BlockInfo BlockInfo(string startMessage, string endMessage, bool logTimeCost = false)
{
return new BlockInfo(startMessage, endMessage, logTimeCost);
}
}
public struct BlockInfo : IDisposable
{
private string endMessage;
private Stopwatch stopwatch;
internal BlockInfo(string startMessage, string endMessage, bool logTimeCost)
{
this.endMessage = endMessage;
if (!string.IsNullOrWhiteSpace(startMessage))
{
LogHelper.Info(startMessage);
}
if (logTimeCost)
{
stopwatch = Stopwatch.StartNew();
stopwatch.Start();
}
else
{
stopwatch = null;
}
}
public void Dispose()
{
string message = null;
if (stopwatch != null)
{
stopwatch.Stop();
message = string.IsNullOrWhiteSpace(endMessage) ? ("[消耗时间:" + stopwatch.Elapsed + "]") : (endMessage + "[消耗时间:" + stopwatch.Elapsed + "]");
}
else
{
message = endMessage;
}
if (!string.IsNullOrWhiteSpace(message))
{
LogHelper.Info(message);
}
}
}
}