日志系统 vb.net
此代码主要是用vb写的
我们在做wince的项目的时候
会用到
直接上代码:
'*******************************************************************************
' クラス名 :TextLog:
' :テキストログファイル
'-------------------------------------------------------------------------------
' 2008/01 :www.szisec.com
'*******************************************************************************
Imports System.Text
Imports System.Configuration
Imports System.IO
Imports System.Windows.Forms
Public Class LocalTextLog
Public Const TYPE_ERROR As String = "E"
Public Const TYPE_STATUS As String = "S"
Public Const TYPE_WARNING As String = "W"
Public Const TYPE_INFO As String = "I"
Public Const TYPE_UPDATA As String = "U"
Protected m_Filename As String = IsecCtl.LogFileName
Protected m_MaxFileSize As Long = IsecCtl.LogFileSize
Protected m_MaxBackUpIndex As Integer = IsecCtl.MaxBackUpIndex
Public Property LogFileName() As String
Get
Return m_Filename
End Get
Set(ByVal Value As String)
m_Filename = Value
End Set
End Property
Public Property MaxFileSize() As Long
Get
Return m_MaxFileSize
End Get
Set(ByVal Value As Long)
If Value > 0 Then
m_MaxFileSize = Value
End If
End Set
End Property
Public Property MaxBackUpIndex() As Integer
Get
Return m_MaxBackUpIndex
End Get
Set(ByVal value As Integer)
If value > 0 Then
m_MaxBackUpIndex = value
End If
End Set
End Property
'***************************************************************************
' 名称 :MakeBackupFileName:バックアップファイル名称作成
' 機能説明 :指定した世代のバックアップ名称を作成する。
' 引数 :age:世代(1から)
' 戻り値 :String:ファイル名称
'***************************************************************************
Private Function MakeBackupFileName(ByVal age As Integer) As String
Dim sb As StringBuilder = New StringBuilder(LogFileName)
sb.Append(".bk")
sb.Append(age)
Return sb.ToString()
End Function
'***************************************************************************
' 名称 :locateLogs:ログファイルのローテーション
' 機能説明 :世代を一つずつずらす。
' 引数 :
' 戻り値 :なし:
'***************************************************************************
Private Sub locateLogs()
Try
If File.Exists(LogFileName) = False Then
System.IO.File.Create(LogFileName).Close()
Exit Sub
End If
Dim nSize As Long = MaxFileSize * 1024
Dim hFileInfo As FileInfo = New System.IO.FileInfo(LogFileName)
If hFileInfo.Length < nSize Then
hFileInfo = Nothing
Exit Sub
End If
hFileInfo = Nothing
Dim nLoopIndex As Integer
Dim sCurrFileName1 As String = ""
Dim sPreFileName2 As String = ""
For nLoopIndex = MaxBackUpIndex To 1 Step -1
sCurrFileName1 = MakeBackupFileName(nLoopIndex)
If nLoopIndex = MaxBackUpIndex Then
If File.Exists(sCurrFileName1) Then
File.Delete(sCurrFileName1)
End If
Else
If File.Exists(sCurrFileName1) Then
File.Move(sCurrFileName1, sPreFileName2)
End If
End If
sPreFileName2 = sCurrFileName1
Next
File.Move(LogFileName, sCurrFileName1)
System.IO.File.Create(LogFileName).Close()
Catch ex As Exception
End Try
End Sub
'***************************************************************************
' 名称 :CreateLogMsg:ログ書き出し用メッセージ作成
' 機能説明 :ログに出力するためのメッセージを組み立てる
' 引数 :msgType:メッセージタイプ
' :msg:業務メッセージ
' :debugInfo:デバッグ情報
' :ex:例外オブジェクト
' 戻り値 :String:メッセージ
'***************************************************************************
Protected Function CreateLogMsg(ByVal msgType As String, ByVal message As String, ByVal debugInfo As String, ByVal ex As Exception) As String
Dim dtNow As DateTime = DateTime.Now
Dim sb As StringBuilder
sb = New StringBuilder("""" & dtNow.ToString & """")
sb.Append(",")
sb.Append("""" & msgType & """")
sb.Append(",")
sb.Append("""" & Path.GetFileName(IsecCtl.AppPath) & """")
sb.Append(",")
sb.Append("""" & message & """")
sb.Append(",")
sb.Append("""" & debugInfo & """")
sb.Append(",")
'処理階層
If (ex Is Nothing) = False Then
sb.Append("""" & ex.StackTrace() & """")
End If
Return sb.ToString()
End Function
'***************************************************************************
' 名称 :Write:ログ出力
' 機能説明 :実際にログ出力する
' 引数 :msg:メッセージ
' 戻り値 :なし:
'***************************************************************************
Protected Sub DoWrite(ByVal msg As String)
LogFileName = IsecCtl.AppPath & IsecCtl.LogFileName
MaxFileSize = BizTypes.SafeStrToLngDef(IsecCtl.LogFileSize, 1024)
MaxBackUpIndex = BizTypes.SafeStrToLngDef(IsecCtl.MaxBackUpIndex, 10)
locateLogs()
' シフトJISにて書き出す
Dim sw As New StreamWriter(LogFileName, True, System.Text.Encoding.UTF8)
Try
sw.WriteLine(msg)
Finally
sw.Close()
sw = Nothing
End Try
End Sub
'***************************************************************************
' 名称 :Warning:ログ書き出し
' 機能説明 :指定した引数のメッセージを組み立ててログを記録する。
' 引数 :msg:業務メッセージ
' :debugInfo:デバッグメッセージ
' 戻り値 :なし:
'***************************************************************************
Public Sub Warning(ByVal msg As String _
, Optional ByVal debugInfo As String = "")
Dim s As String = CreateLogMsg(TYPE_WARNING, msg, debugInfo, Nothing)
Me.DoWrite(s)
End Sub
'***************************************************************************
' 名称 :Trace:ログ書き出し
' 機能説明 :指定した引数のメッセージを組み立ててログを記録する。
' 引数 :msg:業務メッセージ
' :debugInfo:デバッグメッセージ
' 戻り値 :なし:
'***************************************************************************
Public Sub Trace(ByVal msg As String _
, Optional ByVal debugInfo As String = "")
Dim s As String = CreateLogMsg(TYPE_STATUS, msg, debugInfo, Nothing)
Me.DoWrite(s)
End Sub
'***************************************************************************
' 名称 :Err:エラーログ書き出し
' 機能説明 :指定した引数のメッセージを組み立ててログを記録する。
' 引数 :msg:業務メッセージ
' :ex:例外情報
' 戻り値 :なし:
'***************************************************************************
Public Sub Err(ByVal msg As String, ByVal ex As IsecException)
Dim s As String = CreateLogMsg(TYPE_ERROR, msg, ex.MesgInfo, ex)
Me.DoWrite(s)
End Sub
Public Sub Err(ByVal msg As String, ByVal ex As Exception)
Dim s As String = CreateLogMsg(TYPE_ERROR, msg, ex.ToString, ex)
Me.DoWrite(s)
End Sub
'***************************************************************************
' 名称 :Info:情報ログ書き出し
' 機能説明 :指定した引数のメッセージを組み立ててログを記録する。
' 引数 :msg:業務メッセージ
' :ex:例外情報
' 戻り値 :なし:
'***************************************************************************
Public Sub Info(ByVal msg As String, Optional ByVal debugInfo As String = "")
Dim s As String = CreateLogMsg(TYPE_INFO, msg, debugInfo, Nothing)
Me.DoWrite(s)
End Sub
'***************************************************************************
' 名称 :UpdataOpMsg:データ操作ログ書き出し
' 機能説明 :指定した引数のメッセージを組み立ててログを記録する。
' 引数 :msg:業務メッセージ
' :ex:例外情報
' 戻り値 :なし:
'***************************************************************************
Public Sub UpdataOpMsg(ByVal msg As String, Optional ByVal debugInfo As String = "")
Dim s As String = CreateLogMsg(TYPE_UPDATA, msg, debugInfo, Nothing)
Me.DoWrite(s)
End Sub
Public Function ErrMsgF(ByVal msg As String, Optional ByVal i_Title As String = "") As System.Windows.Forms.DialogResult
Return MessageBox.Show(msg, i_Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
'Return MessageBox.Show(msg, i_Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Function
Public Function InfoMsgF(ByVal msg As String, Optional ByVal i_Title As String = "") As System.Windows.Forms.DialogResult
Return MessageBox.Show(msg, i_Title, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button2)
'Return MessageBox.Show(msg, i_Title, MessageBoxButtons.OK, MessageBoxIcon.Information)
End Function
Public Function WarningMsgF(ByVal msg As String, Optional ByVal i_Title As String = "") As System.Windows.Forms.DialogResult
Return MessageBox.Show(msg, i_Title, MessageBoxButtons.OKCancel, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button3)
'Return MessageBox.Show(msg, i_Title, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)
End Function
End Class

浙公网安备 33010602011771号