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> ''' ログレベル ''' </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> ''' ログアクセスクラス ''' </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 ' コンストラクタ Public Sub New(ByVal preFix As String) m_strPreFix = preFix End Sub ''' <summary> ''' ログ出力 ''' </summary> ''' <param name="msg">メッセージ</param> ''' <param name="ex">Exception(無指定の場合はメッセージのみ出力)</param> ''' <remarks></remarks> Public Sub WriteLog( _ ByVal msg As String, Optional ByVal ex As Exception = Nothing) Try ' 設定ファイル取得 Dim clsProf As cls_Profile = fnc_GetProfile() clsProf.SetSection(__ROOT_SECTION__) ' ログフォルダ名取得 Dim logFolder As String = clsProf.GetProfileString(__LOG_DIR__) If logFolder = "" Then ' ログフォルダ名作成 logFolder = System.AppDomain.CurrentDomain.BaseDirectory & "Log" End If ' ログフォルダ名作成 System.IO.Directory.CreateDirectory(logFolder) ' ログファイル名作成 Dim dt As DateTime = Now Dim logFile As String = logFolder & "\" & m_strPreFix & dt.ToString("dd") & __LOG_EXT__ ' 前月翌日分のログファイル削除(1ヶ月分のログファイルしか保存しないようにするため) Dim logNext As String = _ logFolder & "\" & m_strPreFix & dt.AddDays(1).ToString("dd") & __LOG_EXT__ System.IO.File.Delete(logNext) ' ログ出力文字列作成 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でログ出力 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 ' HT名が分かっていれば出力する If pstrHTNo Is Nothing OrElse pstrHTNo = "" Then strMsg &= "[ ]" Else strMsg &= "[" & pstrHTNo & "]" End If strMsg = strMsg & msg WriteLog(strMsg, ex) End Sub End Class ''' <summary> ''' ログファイル出力 ''' </summary> ''' <param name="pre_fix">ログファイルの接頭語</param> ''' <param name="kind">ログレベル</param> ''' <param name="msg">出力文字列</param> ''' <param name="ex">エクセプション</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