C# Snippets


Debug_Flag

Public Shared Function Debug_Flag(ByVal sKey As String) As Boolean

Try
Dim regKey As Microsoft.Win32.RegistryKey
Dim objObject As Object
Dim bValue As Boolean

regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software")
regKey = regKey.OpenSubKey("VB and VBA Program Settings")
regKey = regKey.OpenSubKey("OfficeSolutions")
regKey = regKey.OpenSubKey(gsOFFICE_SOLUTION_GENERAL)

If IsNothing(regKey) = True Then
'has not been installed correctly
DebugFlag = False
Else
objObject = regKey.GetValue(sKey)
If IsNothing(objObject) = False Then
bValue = CType(objObject, Boolean)
DebugFlag = bValue
Else
DebugFlag = False
End If
End If

Catch objCOMException As System.Runtime.InteropServices.COMException
gobjCOMException = objCOMException
Catch objException As Exception
gobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((IsNothing(gobjCOMException) = False Or IsNothing(gobjException) = False)) Then

Call clsError.Handle("DebugFlag", "clsError", _
"return the debug flag from the registry.", _
gobjCOMException, gobjException)
End If
End Try
End Function
'****************************************************************************************

LogFile_Available

Public Function LogFile_Available(ByVal objsettingsfile As clsSettingsFile) As String

Dim sfilepath As String = ""

Try
sfilepath = objsettingsfile.UserProfileFolder & objsettingsfile.LogFileName1
If modGeneral.File_AbleToWriteTo(sfilepath) = True Then
objsettingsfile.LogFileNameAndExtension = objsettingsfile.LogFileName1
Return sfilepath
End If

sfilepath = objsettingsfile.UserProfileFolder & objsettingsfile.LogFileName2
If modGeneral.File_AbleToWriteTo(sfilepath) = True Then
objsettingsfile.LogFileNameAndExtension = objsettingsfile.LogFileName2
Return sfilepath
End If

sfilepath = objsettingsfile.UserProfileFolder & objsettingsfile.LogFileName3
If modGeneral.File_AbleToWriteTo(sfilepath) = True Then
objsettingsfile.LogFileNameAndExtension = objsettingsfile.LogFileName3
Return sfilepath
End If

Call modMessages.LogFile_NotAccessible(objsettingsfile)
Return ""

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
Return ""
End Try
End Function

LogFile_Purge

Public Sub LogFile_Purge(ByVal objsettingsfile As clsSettingsFile)

Dim sfolderpath As String
Dim lngFileSize As Long
Dim iLinesToDelete As Integer
Dim objFileInfo As System.IO.FileInfo
Dim objContentsList As System.Collections.Generic.List(Of String)

Try
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

sfolderpath = LogFile_Available(objsettingsfile)

If modGeneral.File_Exists(sfolderpath, True) = True Then

objFileInfo = New System.IO.FileInfo(sfolderpath)
lngFileSize = objFileInfo.Length

'if log file size is greater than 3MB
If lngFileSize > 3146268 Then
objContentsList = System.IO.File.ReadAllLines(sfolderpath).ToList

iLinesToDelete = CType(objContentsList.Count / 2, Integer)
objContentsList.RemoveRange(0, iLinesToDelete - 1)

System.IO.File.WriteAllLines(sfolderpath, objContentsList.ToArray)
End If
End If

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Sub

Tracer_Add

Public Sub Tracer_Add2(ByVal sCategory As String, _
ByVal sTracerMessage As String, _
Optional ByVal bAlwaysLogged As Boolean = False, _
Optional ByVal objMethodBase As System.Reflection.MethodBase = Nothing)

Dim smessage As String = ""
Dim sdatetime As String = ""
Dim sSubroutineName As String = ""
Dim sDeclaringType As String = ""
Dim sClassName As String = ""

Try
If (sCategory = "REGISTRY") Or _
(sCategory = "SYSTEM") Or _
(sCategory = "SERVICE") Or _
(sCategory = "PROGRESS") Or _
(sCategory = "EXCEPTION") Or _
(sCategory = "WARNING") Or _
(sCategory = "WORD") Or _
(sCategory = "MENU") Or _
(sCategory = "TOOLBAR") Or _
(sCategory = "COMMANDBAR") Or _
(sCategory = "FORM") Or _
(sCategory = "MESSAGE") Or _
(sCategory = "QUESTION") Then
bAlwaysLogged = True
End If

'Used for quick debugging
'If (sCategory = "SUBROUTINE") Then
' bAlwaysLogged = True
'End If

If (objMethodBase IsNot Nothing) Then
sSubroutineName = objMethodBase.Name
sDeclaringType = objMethodBase.DeclaringType.ToString()
sClassName = sDeclaringType.Substring(sDeclaringType.IndexOf(".") + 1)
End If

If Not (gSettingsFile Is Nothing) Then
'do not log it unless the verbose logging option is selected
If (gSettingsFile.DisplayDetailedLogging = False And bAlwaysLogged = False) Then
Exit Sub
End If
End If

sdatetime = DateTime_ToSensibleFormat(System.DateTime.Now, True)
'sdatetime = Microsoft.VisualBasic.Now.ToString("yyyy-MM-dd HH:mm:ss ffff")

If (sClassName.Length > 0) Then
smessage &= "(" & sClassName & " - " & sSubroutineName & ") "
End If

smessage &= sTracerMessage

If (sCategory.Length > 0) Then

If (sCategory = "QUESTION" Or sCategory = "MESSAGE") Then
System.Diagnostics.Trace.WriteLine("")
End If

smessage = sCategory & ": " & smessage
System.Diagnostics.Trace.WriteLine(smessage, sdatetime)

If (sCategory = "QUESTION" Or sCategory = "MESSAGE") Then
System.Diagnostics.Trace.WriteLine("")
End If
Else
If (smessage.Length > 0) Then
System.Diagnostics.Trace.WriteLine(smessage)
Else
System.Diagnostics.Trace.WriteLine("")
End If
End If

Catch ex As Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex, _
"", False)
End Try
End Sub

Tracer_AddService

Public Sub Tracer_AddService(ByVal sServiceName As String, _
ByVal objServiceObject As Object, _
ByVal enOperation As enSERVICES, _
ByVal sMethodName As String, _
ByVal objMethodBase As System.Reflection.MethodBase, _
Optional ByVal objArgument As Object = Nothing)

Dim stypedescription As String = ""
Dim swebserviceaddress As String = ""
Dim sSubroutineName As String = ""
Dim sDeclaringType As String = ""
Dim sClassName As String = ""
Dim objDocviewUploadService2 As DocviewUploadService2.DocviewUploadService2PortClient
Dim objCFService As CFService.CFServicePortClient

Try
Select Case enOperation
Case enSERVICES.INITIALISE : stypedescription = "Initialise"
Case enSERVICES.REQUEST_START : stypedescription = "Request Start"
Case enSERVICES.REQUEST_FINISH : stypedescription = "Request Finish"
Case enSERVICES.COMPLETED_START : stypedescription = "Completed Start"
Case enSERVICES.COMPLETED_FINISH : stypedescription = "Completed Finish"
End Select

If (objServiceObject Is Nothing) Then
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, Nothing, _
"Unable to write the following line to the tracer because the 'objServiceObject' is set to Nothing" & _
System.Environment.NewLine & _
sServiceName & " - " & sMethodName & " (" & stypedescription & ")", _
False)
Exit Sub
End If

If (objMethodBase IsNot Nothing) Then
sSubroutineName = objMethodBase.Name
sDeclaringType = objMethodBase.DeclaringType.ToString()
sClassName = sDeclaringType.Substring(sDeclaringType.IndexOf(".") + 1)
End If

Select Case sServiceName
Case "DocView"
objDocviewUploadService2 = CType(objServiceObject, DocviewUploadService2.DocviewUploadService2PortClient)
swebserviceaddress = objDocviewUploadService2.Endpoint.Address.ToString()

Case "Credit"
objCFService = CType(objServiceObject, CFService.CFServicePortClient)
swebserviceaddress = objCFService.Endpoint.Address.ToString()
End Select

Call Tracer_Add2("SERVICE", sServiceName & " - URL - " & swebserviceaddress & " - " & sMethodName & " (" & stypedescription & ")")
Call Tracer_Add2("SERVICE", "(" & sClassName & " - " & sSubroutineName & ")")

Catch ex As Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Sub

Tracer_AddSubroutineFinish

Public Sub Tracer_AddSubroutineFinish(ByVal objMethodBase As System.Reflection.MethodBase)
Dim sSubroutineName As String = ""
Dim sDeclaringType As String = ""
Dim sClassName As String = ""

Try
If (objMethodBase IsNot Nothing) Then
sSubroutineName = objMethodBase.Name
sDeclaringType = objMethodBase.DeclaringType.ToString()
sClassName = sDeclaringType.Substring(sDeclaringType.IndexOf(".") + 1)
End If

Call Tracer_Add2("SUBROUTINE", "(" & sClassName & " - " & sSubroutineName & ") finish")

Catch ex As Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Sub

Tracer_AddSubroutineStart

Public Sub Tracer_AddSubroutineStart(ByVal objMethodBase As System.Reflection.MethodBase, _
Optional ByVal sAdditionalText As String = "")
Dim sSubroutineName As String = ""
Dim sDeclaringType As String = ""
Dim sClassName As String = ""

Try
If (objMethodBase IsNot Nothing) Then
sSubroutineName = objMethodBase.Name
sDeclaringType = objMethodBase.DeclaringType.ToString()
sClassName = sDeclaringType.Substring(sDeclaringType.IndexOf(".") + 1)
End If

Call Tracer_Add2("SUBROUTINE", "(" & sClassName & " - " & sSubroutineName & ") start " & sAdditionalText)

Catch ex As Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Sub

Tracer_Finish

Public Sub Tracer_Finish(ByVal sApplication As String)

Dim stracermessage As String = ""

Try
Call Tracer_Add2("", "")
stracermessage &= "----------------" & sApplication.ToUpper & " SESSION ENDED: "
stracermessage &= modGeneral.DateTime_ToSensibleFormat(System.DateTime.Now, True, True)
stracermessage &= " ---------------"

System.Diagnostics.Trace.WriteLine(stracermessage)
Call Tracer_Add2("", "")

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Sub

Tracer_Start

Public Sub Tracer_Start(ByVal sApplication As String, _
ByRef objSettingsFile As clsSettingsFile)

Dim stracermessage As String = ""
Dim sfilepath As String
Dim objStreamWriter As System.IO.StreamWriter
Dim objTraceListener As System.Diagnostics.TextWriterTraceListener

Try
sfilepath = objSettingsFile.UserProfileFolder & objSettingsFile.LogFileNameAndExtension

objStreamWriter = System.IO.File.AppendText(sfilepath)
objTraceListener = New System.Diagnostics.TextWriterTraceListener(objStreamWriter)

System.Diagnostics.Trace.AutoFlush = True
System.Diagnostics.Trace.Listeners.Add(objTraceListener)

Call Tracer_Add2("", "")

stracermessage &= "----------------" & sApplication.ToUpper & " SESSION STARTED: "
stracermessage &= modGeneral.DateTime_ToSensibleFormat(System.DateTime.Now, True, True)
stracermessage &= " ---------------"

System.Diagnostics.Trace.WriteLine(stracermessage)
System.Diagnostics.Trace.WriteLine("Add-in version: " & _
gsVERSION_VSRELEASE & _
" (" & My.Application.Info.Version.ToString & ")")
System.Diagnostics.Trace.WriteLine("User Name: " & _
System.Windows.Forms.SystemInformation.UserName)
System.Diagnostics.Trace.WriteLine("Region: " & _
System.Globalization.RegionInfo.CurrentRegion.Name & _
" (" & System.Globalization.RegionInfo.CurrentRegion.EnglishName & ")")
System.Diagnostics.Trace.WriteLine("Language: " & _
System.Globalization.CultureInfo.CurrentCulture.EnglishName)
Call Tracer_Add2("", "")

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Sub

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited Top