SelfHelpMachine.LogHelper
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
namespace SelfHelpMachine
{
public class LogHelper
{
#region Instance
private static object logLock;
private static LogHelper _instance;
private static string logFileName;
private LogHelper() { }
/// <summary>
/// LogHelper instance
/// </summary>
public static LogHelper Instance
{
get
{
if (_instance == null)
{
_instance = new LogHelper();
logLock = new object();
logFileName = "自助机日志" + ".log";
}
return _instance;
}
}
#endregion
/// <summary>
///
/// </summary>
/// <param name="logContent"></param>
/// <param name="logType"></param>
/// <param name="fileName"></param>
public void WriteLog(string logContent, LogEnum logType = LogEnum.Information, string fileName = null)
{
try
{
string basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
//basePath = @"C:\APILogs";
if (!Directory.Exists(basePath + "\\Log"))
{
Directory.CreateDirectory(basePath + "\\Log");
}
string dataString = DateTime.Now.ToString("yyyy-MM-dd");
if (!Directory.Exists(basePath + "\\Log\\" + dataString))
{
Directory.CreateDirectory(basePath + "\\Log\\" + dataString);
}
string[] logText = new string[] { DateTime.Now.ToString("HH:mm:ss") + ": " + logType.ToString() + ": " + logContent };
if (!string.IsNullOrEmpty(fileName))
{
fileName = fileName + "_" + logFileName;
}
else
{
fileName = logFileName;
}
lock (logLock)
{
File.AppendAllLines(basePath + "\\Log\\" + dataString + "\\" + fileName, logText);
}
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
///
/// </summary>
/// <param name="exception"></param>
/// <param name="specialText"></param>
public void WriteException(Exception exception, string specialText = null)
{
if (exception != null)
{
Type exceptionType = exception.GetType();
string text = string.Empty;
if (!string.IsNullOrEmpty(specialText))
{
text = text + specialText + Environment.NewLine;
}
text = "Exception: " + exceptionType.Name + Environment.NewLine;
text += " " + "Message: " + exception.Message + Environment.NewLine;
text += " " + "Source: " + exception.Source + Environment.NewLine;
text += " " + "StackTrace: " + exception.StackTrace + Environment.NewLine;
WriteLog(text, LogEnum.Error);
}
}
public void WriteUIException(DispatcherUnhandledExceptionEventArgs exception, string specialText = null)
{
if (exception != null)
{
Type exceptionType = exception.GetType();
string text = string.Empty;
if (!string.IsNullOrEmpty(specialText))
{
text = text + specialText + Environment.NewLine;
}
text = "ExceptionFORUI: " + exceptionType.Name + Environment.NewLine;
text += " " + "Message: " + exception.Exception.Message + Environment.NewLine;
text += " " + "Source: " + exception.Exception.Source + Environment.NewLine;
text += " " + "StackTrace: " + exception.Exception.StackTrace + Environment.NewLine;
WriteLog(text, LogEnum.Error);
}
}
public void WriteMechineLog(string message, string note, string encounterid ="-1000")
{
string MachineCode = ConfigHelper.ThisMachineID;
string sql = $"Insert into prpa.SelfHelpMachineLog (ENCOUNTERID,MESSAGE,NOTE,MACHINECODE,LOGTIME) values ({encounterid},{message},{note},{MachineCode},to_date('2021-08-24 10:16:27','YYYY-MM-DD HH24:MI:SS'))";
BaseDataHelper.QueryDataBase(BaseDataHelper._connectionString, sql);
}
}
}
浙公网安备 33010602011771号