• vb.net 写日志文件


    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
    

      

  • 相关阅读:
    产品化软件开发与项目化软件开发的对比
    4.ThinkPHP 3.1.2 输出和模型使用
    ThinkPHP 3.1.2 输出和模型使用1
    事务管理配置与@Transactional注解使用
    logstash 区分多个文件index端配置
    logstash 读取多个系统相同文件shipper端
    centos 6.5安装git
    如何查看PHP的配置信息
    MVC模式和URL访问
    1.环境搭建
  • 原文地址:https://www.cnblogs.com/sxjljj/p/8889076.html
Copyright © 2020-2023  润新知