写Log类

Imports  System
Imports  System.IO
Imports  System.Globalization
Imports  System.Text
Imports  System.Configuration

Namespace Log
    
' ************************************************************************
    '
    ' 
    ' クラス名 :Write LogFile 
    ' 種別     :フォーム
    ' 用途     :LogfileIO
    ' 備考     :なし
    ' 履歴     :
    ' 日付        バージョン                  名前          内容
    '           2006/09/05  00.01            ronglj       新規作成
    '
    '*************************************************************************
    Public Class LogFileIO

        
Private Const LOGFilePath As String = "\LOG" 'log file path
        '------------------------------------------------------------------
        ' 機能     ; get the LogFile Path
        ' 戻り値   ; DataTable
        '               正常の場合:string 
        '               異常の場合:Nothing
        ' 引き数   ; 無し
        ' 機能説明 ; 
        ' 備考     ;
        '-------------------------------------------------------------------
        Private Function getLogPath() As String

            
Return getAppAppSetting("LogPath")
        
End Function


        
'------------------------------------------------------------------
        ' 機能     ; getApplicacion 
        ' 戻り値   ; DataTable
        '               正常の場合:string 
        '               異常の場合:Nothing
        ' 引き数   ; 無し
        ' 機能説明 ; 
        ' 備考     ;
        '-------------------------------------------------------------------
        Private Function getAppAppSetting(ByVal keyName As StringAs String

            
Dim keyvalue As String

            keyvalue 
= ""
            
If (System.Configuration.ConfigurationManager.AppSettings(keyName) Is NothingThen

                keyvalue 
= ""
            
Else

                keyvalue 
= System.Configuration.ConfigurationManager.AppSettings(keyName)
            
End If

            
Return keyvalue
        
End Function


        
'------------------------------------------------------------------
        ' 機能     ; get File Save Directory  
        ' 戻り値   ; DataTable
        '               正常の場合:string 
        '               異常の場合:Nothing
        ' 引き数   ; 無し
        ' 機能説明 ; 
        ' 備考     ;
        '-------------------------------------------------------------------
        Private Function getFileSaveDirectory() As String

            
Dim path As String

            
'path = System.Web.HttpContext.Server.MapPath("\")
            path = getLogPath()

            
If path.Trim = "" Then
                
Return ""
            
End If

            
'return the path
            Return path
        
End Function


        
'------------------------------------------------------------------
        ' 機能     ; set LogFile Name  
        ' 戻り値   ; DataTable
        '               正常の場合:string 
        '               異常の場合:Nothing
        ' 引き数   ; 無し
        ' 機能説明 ; 
        ' 備考     ;
        '-------------------------------------------------------------------
        Private Function getLogFileName() As String

            
Dim filename As String
            
Dim dt As DateTime = DateTime.Now

            filename 
= getAppAppSetting("LogName"& dt.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo) & ".log"

            
Return filename
        
End Function


        
'------------------------------------------------------------------
        ' 機能     ; write LogFile  
        ' 戻り値   ; DataTable
        '               正常の場合:true 
        '               異常の場合:flase
        ' 引き数   ; 無し
        ' 機能説明 ; 
        ' 備考     ;
        '-------------------------------------------------------------------
        Public Function WriteLogFile(ByVal LogInfo As StringAs Boolean

            
Dim file As System.IO.StreamWriter
            
Dim filenew As System.IO.FileStream
            
Dim dir_exists As Boolean
            
Dim file_exists As Boolean
            
Dim logDirectory As String
            
Dim logFileName As String
            
Dim logFileFullDirectory As String
            
Dim LoginStr As String
            
Dim dt As DateTime = DateTime.Now
            
Dim dtinfo As String = dt.ToString("yyyy/MM/dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo) & "   "

            
Try

                
If getAppAppSetting("LogOutFlag").ToUpper <> "ON" Then
                    
Return True
                
End If

                
If getAppAppSetting("SuccessLogOutFlag").ToUpper <> "ON" Then

                    
If LogInfo.IndexOf("実行失敗"<= 0 Or LogInfo.IndexOf("実行失敗"> 200 Then
                        
Return True
                    
End If
                
End If

                logDirectory 
= getFileSaveDirectory()

                
'if it has no value do not write  logs
                If logDirectory.Trim = "" Then
                    
Return True
                
End If

                
If Right(logDirectory, 1= "\" Then
                    logDirectory 
= logDirectory.Substring(0, logDirectory.Length - 1)
                
End If
                logFileName 
= getLogFileName()

                
'get the Directory Exists
                dir_exists = System.IO.Directory.Exists(logDirectory)

                logFileFullDirectory 
= logDirectory & "\" & logFileName

                LoginStr 
= dtinfo & LogInfo

                
If dir_exists Then
                    
'have the log Directory
                    file_exists = System.IO.File.Exists(logFileFullDirectory)

                    
If file_exists Then
                        
'have that day log file
                        file = New System.IO.StreamWriter(logFileFullDirectory, True)
                        file.WriteLine(LoginStr)
                        file.Close()
                    
Else
                        file 
= New System.IO.StreamWriter(logFileFullDirectory)
                        file.WriteLine(LoginStr)
                        file.Close()
                    
End If

                
Else
                    
'ログファイルの所在するパス名
                    System.IO.Directory.CreateDirectory(logDirectory)

                    
'create a new file and close it
                    filenew = System.IO.File.Create(logFileFullDirectory)
                    filenew.Close()

                    
'ログファイルを開いて、Append Modeで引数の「ログの出力内容」を書き込む
                    file = New System.IO.StreamWriter(logFileFullDirectory, True, System.Text.Encoding.GetEncoding("SHIFT_JIS"))
                    file.WriteLine(LoginStr)
                    
'ログファイルを閉じる
                    file.Close()
                
End If

                
'it is cussecful
                Return True
            
Catch ex As Exception

                
Return False
            
End Try
        
End Function


    
End Class


End Namespace

你可能感兴趣的:(log)