using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace SPC.Kiosk.Common
{
///
/// Common Logging Class
///
public static class CommonLog
{
///
/// Info Log Write
///
///
///
///
///
///
public static void InfoLogWrite(string _nameSpace, string _className, string _functionName, string _logString, string _subString = "")
{
try
{
InfoLogWrite(string.Format("{0}.{1} {2} {3}", _nameSpace, _className, _functionName, _logString), _subString);
}
catch
{
}
}
///
/// Info Log Write
///
///
///
///
///
public static void InfoLogWrite(object _sendClass, string _functionName, string _logString="", string _subString = "")
{
try
{
LogWrite(Log_Type.Info, _sendClass, _functionName, _logString, _subString);
}
catch
{
}
}
///
/// Info Log Write
///
///
///
public static void InfoLogWrite(string _logString, string _subString = "")
{
try
{
LogWrite(Log_Type.Info, _logString, _subString);
}
catch
{
}
}
///
/// Error Log Write
///
///
///
///
///
///
public static void ErrorLogWrite(string _nameSpace, string _className, string _functionName, string _errorLogString, string _errorSubString = "")
{
try
{
ErrorLogWrite(string.Format("{0}.{1} {2} {3}", _nameSpace, _className, _functionName, _errorLogString), _errorSubString);
}
catch
{
}
}
///
/// Error Log Write
///
///
///
///
///
public static void ErrorLogWrite(object _sendClass, string _functionName, string _errorLogString="", string _errorSubString = "")
{
try
{
LogWrite(Log_Type.Error, _sendClass, _functionName, _errorLogString, _errorSubString);
}
catch
{
}
}
///
/// Error Log Write
///
///
///
public static void ErrorLogWrite(string _errorLogString, string _errorSubString = "")
{
try
{
LogWrite(Log_Type.Error, _errorLogString, _errorSubString);
}
catch
{
}
}
///
/// Bebug Log Write
///
///
///
///
///
///
public static void DebugLogWrite(string _nameSpace, string _className, string _functionName, string _debugLogString, string _debugSubString = "")
{
try
{
DebugLogWrite(string.Format("{0}.{1} {2} {3}", _nameSpace, _className, _functionName, _debugLogString), _debugSubString);
}
catch
{
}
}
///
/// Bebug Log Write
///
///
///
///
///
public static void DebugLogWrite(object _sendClass, string _functionName, string _debugLogString="", string _debugSubString = "")
{
try
{
LogWrite(Log_Type.Debug, _sendClass, _functionName, _debugLogString, _debugSubString);
}
catch
{
}
}
///
/// Bebug Log Write
///
///
///
public static void DebugLogWrite(string _debugLogString, string _debugSubString = "")
{
try
{
LogWrite(Log_Type.Debug, _debugLogString, _debugSubString);
}
catch
{
}
}
///
/// Log Write
///
///
///
///
///
///
private static void LogWrite(Log_Type _logType, object sendClass, string _functionName, string _logString, string _subString)
{
try
{
var type = sendClass.GetType();
LogWrite(_logType, string.Format("{0}.{1} {2} {3}", type.Namespace, type.Name, _functionName, _logString), _subString);
}
catch
{
LogWrite(_logType, string.Format("{0} {1}", _functionName, _logString), _subString);
}
}
///
/// Log Write
///
///
///
///
private static void LogWrite(Log_Type _logType, string _logString,string _logSubString)
{
try
{
using (var oLog = new logging(CommonValue.KioskLogSetting))
{
oLog.LogWrite
(
new Log_Message
{
LogDate = DateTime.Now,
LogFrom = CommonValue.LogName,
LogType = _logType,
LogMessage = string.IsNullOrEmpty(_logString)
? _logString
: string.Format("{0}\n\t{1}", _logString, _logSubString),
}
);
}
}
catch
{
}
}
}
#region Log Writer Class
///
/// Log Store Object
///
public class logging : IDisposable
{
private Log_Setting Log_CFG; //Object Configuration Infomation
private DateTime LastLogTime; //Last Log Create Time
private String _LogPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log");
///
/// Log Configuration Setting
///
public Log_Setting Log_Set
{
get { return Log_CFG; }
set { Log_CFG = value; }
}
///
/// logging a Message object
///
///
public logging(Log_Setting _LogCFG)
{
Log_Set = _LogCFG;
if (!Log_Set.LogPath.Equals(string.Empty) && !Log_Set.LogName.Equals(string.Empty))
{
if (Log_Set.LogPath.IndexOf('\\') > -1)
{
_LogPath = Path.Combine(Log_Set.LogPath, Log_Set.LogName);
}
else
{
_LogPath = Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Log_Set.LogPath), Log_Set.LogName);
}
}
else if (!Log_Set.LogPath.Equals(string.Empty))
{
if (Log_Set.LogPath.IndexOf('\\') > -1)
{
_LogPath = Log_Set.LogPath;
}
else
{
_LogPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Log_Set.LogPath);
}
}
else
{
_LogPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ".Log");
}
if (!Directory.Exists(_LogPath)) Directory.CreateDirectory(_LogPath);
LastLogTime = DateTime.Now.AddDays(-1F);
}
public void Dispose()
{
}
///
/// logging function
///
/// log data
public void LogWrite(Log_Message _LogData)
{
int retryCount = 0;
bool result = false;
do
{
result = LogWriteToFile(_LogData);
retryCount++;
} while (!result && retryCount < 100);
if (!result)
{
Console.WriteLine(string.Format("Log Write Fail:{0}", _LogData.LogMessage));
}
}
///
/// Log Write Internal Function
///
///
///
private bool LogWriteToFile(Log_Message _LogData)
{
bool result = false;
try
{
if (Array.IndexOf(Log_Set.LogFrom, _LogData.LogFrom) > -1
&& Array.IndexOf(Log_Set.LogType, _LogData.LogType) > -1)
{
string _filePath = Path.Combine(_LogPath, string.Format("{0}_{1}.log", _LogData.LogFrom, _LogData.LogDate.ToString("yyyyMMdd")));
result = rTextFileWrite(string.Format("{0},{1},{2},{3}\r\n"
, _LogData.LogDate.ToString("yyyy-MM-dd HH:mm:ss.fff")
, _LogData.LogFrom
, _LogData.LogType
, _LogData.LogMessage), _filePath, true);
if (LastLogTime.Date != DateTime.Now.Date)
{
DateTime _chkDate = DateTime.Now.AddDays(Log_Set.LogDates * -1);
string[] getFiles = Directory.GetFiles(_LogPath);
foreach (string aFile in getFiles)
{
if (File.GetCreationTime(aFile).Date < _chkDate.Date)
{
File.Delete(aFile);
}
}
}
LastLogTime = _LogData.LogDate;
}
else
{
result = true;
}
}
catch
{
}
return result;
}
///
/// String Add to File
///
///
///
///
///
public bool rTextFileWrite(string _dataString, string _filePath, bool _append)
{
bool result = false;
try
{
using (StreamWriter sw = new StreamWriter(_filePath, _append))
{
sw.Write(_dataString);
sw.Flush();
sw.Close();
}
result = true;
}
catch
{
result = false;
}
return result;
}
}
#endregion
}