#region LogManager 日志测试
private void WriteLogTest()
{
LogManager.WriteErrorLog(new Exception("WriteErrorLog"));
LogManager.WriteGlobalLog("WriteGlobalLog");
LogManager.WriteLog("WriteLog");
LogManager.WriteDeviceJson("WriteDeviceJson", "DeviceJson");
LogManager.WriteSysErrorLog("WriteSysErrorLog");
LogManager.WriteSysWarningLog("WriteSysWarningLog");
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FX5UTest
{
public class LogManager
{
public static string LogPath
{
get
{
if (LogManager.logPath == string.Empty)
{
LogManager.logPath = AppDomain.CurrentDomain.BaseDirectory + "log\\";
if (!Directory.Exists(LogManager.logPath))
{
Directory.CreateDirectory(LogManager.logPath);
}
}
return LogManager.logPath;
}
set
{
LogManager.logPath = value;
}
}
public static string DevicePath
{
get
{
if (LogManager.devicePath == string.Empty)
{
LogManager.devicePath = AppDomain.CurrentDomain.BaseDirectory + "DeviceData\\";
if (!Directory.Exists(LogManager.devicePath))
{
Directory.CreateDirectory(LogManager.devicePath);
}
}
return LogManager.devicePath;
}
set
{
LogManager.devicePath = value;
}
}
public static string ErrorPath
{
get
{
if (LogManager.errorPath == string.Empty)
{
LogManager.errorPath = AppDomain.CurrentDomain.BaseDirectory + "errorLog\\";
if (!Directory.Exists(LogManager.errorPath))
{
Directory.CreateDirectory(LogManager.errorPath);
}
}
return LogManager.errorPath;
}
set
{
LogManager.errorPath = value;
}
}
public static string LogFilePrefix
{
get
{
return LogManager.logFilePrefix;
}
set
{
LogManager.logFilePrefix = value;
}
}
public static void WriteDeviceJson(string msg, string fileName)
{
try
{
if (!Directory.Exists(LogManager.DevicePath))
{
Directory.CreateDirectory(LogManager.DevicePath);
}
StreamWriter streamWriter = new StreamWriter(LogManager.DevicePath + fileName + ".json", false, Encoding.UTF8);
streamWriter.Write(msg);
streamWriter.Flush();
streamWriter.Close();
}
catch
{
}
}
public static void WriteLog(string logFile, string msg)
{
try
{
StreamWriter streamWriter = File.AppendText(string.Concat(new string[]
{
LogManager.LogPath,
logFile,
"_",
DateTime.Now.ToString("yyyyMMdd"),
".Log"
}));
streamWriter.WriteLine(string.Concat(new string[]
{
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff "),
"from:",
" ",
msg
}));
streamWriter.Close();
}
catch
{
}
}
public static void WriteErrorMessageLog( string msg)
{
try
{
//if (!Directory.Exists(LogManager.errorPath))
//{
// Directory.CreateDirectory(LogManager.errorPath);
//}
StreamWriter streamWriter = File.AppendText(string.Concat(new string[]
{
LogManager.ErrorPath,
LogManager.LogFilePrefix,
"Error",
"_",
DateTime.Now.ToString("yyyyMMdd"),
".Log"
}));
streamWriter.WriteLine(string.Concat(new string[]
{
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff "),
"from:",
" ",
msg
}));
streamWriter.Close();
}
catch
{
}
}
public static void WriteGlobalLog(string msg)
{
try
{
//if (!Directory.Exists(LogManager.LogPath))
//{
// Directory.CreateDirectory(LogManager.LogPath);
//}
string text = "log";
StreamWriter streamWriter = File.AppendText(string.Concat(new string[]
{
LogManager.LogPath,
"Global_",
text,
"_",
DateTime.Now.ToString("yyyyMMdd"),
".Log"
}));
streamWriter.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff ") + "from:" + msg);
streamWriter.Close();
}
catch
{
}
}
public static void WriteLog(string msg)
{
try
{
if (!Directory.Exists(LogManager.LogPath))
{
Directory.CreateDirectory(LogManager.LogPath);
}
string text2 = "log";
StreamWriter streamWriter = File.AppendText(string.Concat(new string[]
{
LogManager.LogPath,
LogManager.LogFilePrefix,
text2,
"_",
DateTime.Now.ToString("yyyyMMdd"),
".Log"
}));
streamWriter.WriteLine(string.Concat(new string[]
{
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff "),
"from:",
" ",
msg
}));
streamWriter.Close();
}
catch
{
}
}
public static void WriteErrorLog( Exception ex)
{
LogManager.WriteErrorMessageLog("------SysDataException------");
LogManager.WriteErrorMessageLog(ex.Message);
LogManager.WriteErrorMessageLog(ex.Source);
LogManager.WriteErrorMessageLog(ex.StackTrace);
LogManager.WriteErrorMessageLog("------SysDataException------");
}
public static void WriteSysWarningLog( string msg)
{
LogManager.WriteLog(SysLogFile.Warning.ToString(), msg);
}
public static void WriteSysErrorLog(string msg)
{
LogManager.WriteLog(SysLogFile.Error.ToString(), msg);
}
private static string logPath = string.Empty;
private static string devicePath = string.Empty;
private static string errorPath = string.Empty;
private static string logFilePrefix = string.Empty;
public enum SysLogFile
{
Trace,
Warning,
Error,
SQL
}
}
}