Module mod_LogAccessHt #Region "Const" Public Const __PREFIX_ERROR__ As String = "Error_" Public Const __PREFIX_TRACE__ As String = "Trace_" Public Const __PREFIX_DEBUG__ As String = "Debug_" Public Const __PREFIX_SQL__ As String = "Sql_" #End Region ''' <summary> '''Log level ''' </summary> ''' <remarks></remarks> Public Enum enumLogLevel As Integer __LOG_LVL_EMERG__ ' system is unusable __LOG_LVL_ALERT__ ' action must be taken immediately __LOG_LVL_CRIT__ ' critical conditions __LOG_LVL_ERR__ ' error conditions __LOG_LVL_WARN__ ' warning conditions __LOG_LVL_NOTE__ ' normal but significant condition __LOG_LVL_INFO__ ' informational __LOG_LVL_DEBUG__ ' debug-level messages __LOG_LVL_SQL__ ' issued SQL string End Enum ''' <summary> '''Log access class ''' </summary> ''' <remarks></remarks> Public Class cls_LogAccess Private Const __LOG_DIR__ As String = "log_dir" Private Const __LOG_EXT__ As String = ".log" Private m_strPreFix As String 'Constructor Public Sub New(ByVal preFix As String) m_strPreFix = preFix End Sub ''' <summary> '''Log output ''' </summary> '''<param name = "msg"> Message </ param> '''<param name = "ex"> Exception (only message is output if not specified) </ param> ''' <remarks></remarks> Public Sub WriteLog( _ ByVal msg As String, Optional ByVal ex As Exception = Nothing) Try 'Get configuration file Dim clsProf As cls_Profile = fnc_GetProfile() clsProf.SetSection(__ROOT_SECTION__) 'Get log folder name Dim logFolder As String = clsProf.GetProfileString(__LOG_DIR__) If logFolder = "" Then 'Create log folder name logFolder = System.AppDomain.CurrentDomain.BaseDirectory & "Log" End If 'Create log folder name System.IO.Directory.CreateDirectory(logFolder) 'Create log file name Dim dt As DateTime = Now Dim logFile As String = logFolder & "\" & m_strPreFix & dt.ToString("dd") & __LOG_EXT__ 'Delete the log file for the next day of the previous month (to save only the log file for one month) Dim logNext As String = _ logFolder & "\" & m_strPreFix & dt.AddDays(1).ToString("dd") & __LOG_EXT__ System.IO.File.Delete(logNext) 'Create log output string Dim logStr As String logStr = dt.ToString("yyyy/MM/dd HH:mm:ss") & vbTab & msg If ex Is Nothing = False Then logStr = logStr & vbCrLf & ex.ToString End If 'Shift-JIS log output Dim sw As IO.StreamWriter = Nothing Try sw = New IO.StreamWriter(logFile, True, _ System.Text.Encoding.GetEncoding("Shift-JIS")) sw.WriteLine(logStr) Catch ex2 As Exception Finally If sw Is Nothing = False Then sw.Close() End Try Catch ex2 As Exception End Try End Sub Public Sub WriteErrorLog(ByVal kind As Integer, ByVal msg As String, Optional ByVal ex As Exception = Nothing) Dim strMsg As String = "" Select Case kind Case enumLogLevel.__LOG_LVL_EMERG__ ' system is unusable strMsg = "[EMERG] " Case enumLogLevel.__LOG_LVL_ALERT__ ' action must be taken immediately strMsg = "[ALART] " Case enumLogLevel.__LOG_LVL_CRIT__ ' critical conditions strMsg = "[CRIT] " Case enumLogLevel.__LOG_LVL_ERR__ ' error conditions strMsg = "[ERROR] " Case enumLogLevel.__LOG_LVL_WARN__ ' warning conditions strMsg = "[WARN] " Case enumLogLevel.__LOG_LVL_NOTE__ ' normal but significant condition strMsg = "[NOTE] " Case enumLogLevel.__LOG_LVL_INFO__ ' informational strMsg = "[INFO] " Case enumLogLevel.__LOG_LVL_DEBUG__ ' debug-level messages strMsg = "[DEBUG] " Case enumLogLevel.__LOG_LVL_SQL__ ' issued SQL string strMsg = "[SQL] " End Select 'Output if the HT name is known If pstrHTNo Is Nothing OrElse pstrHTNo = "" Then strMsg &= "[ ]" Else strMsg &= "[" & pstrHTNo & "]" End If strMsg = strMsg & msg WriteLog(strMsg, ex) End Sub End Class ''' <summary> '''Log file output ''' </summary> '''<param name = "pre_fix"> Log file prefix </ param> '''<param name = "kind"> log level </ param> ''' <param name="msg">Output text line</param> '''<param name = "ex"> Exception </ param> ''' <remarks></remarks> Public Sub sub_WriteErrorLog(ByVal pre_fix As String, ByVal kind As Integer, ByVal msg As String, Optional ByVal ex As Exception = Nothing) Dim clsLog As cls_LogAccess = New cls_LogAccess(pre_fix) clsLog.WriteErrorLog(kind, msg, ex) End Sub End Module