Solved

Logging utility

Posted on 2008-06-24
4
238 Views
Last Modified: 2010-05-18
Logging utility to log the events that occur in an application. This utility can be called from any part of the application.

0
Comment
Question by:PradeepYadhav
4 Comments
 
LVL 22

Expert Comment

by:cookre
ID: 21856910
Take a look at System.Diagnostics.EventLog
0
 
LVL 10

Accepted Solution

by:
oxyoo earned 500 total points
ID: 21856926
Hi take a look at the following log libraries that have an extensive feature list.

Log4Net
http://logging.apache.org/log4net/

Enterprise Library - Logging Application Block
http://msdn.microsoft.com/en-us/library/cc511708.aspx

Good Luck
0
 
LVL 7

Expert Comment

by:nsanga
ID: 21857189
copy the code to some file and add the file to your project.....

to use it.....

1) include the namespace MyLog
2) include this line at the start of the application

Tracing.OpenLog()

3) then to write the debug

Tracing.DebugPrint("simple log")

4) at the end of the application call
Tracing.CloseLog()

you can avoid OpenLog by keeping the call in shared new() of the Tracing class
and to avoid CloseLog() keep call in finalize





Option Explicit On

Option Strict On
 

Namespace MyLog
 

    Public Enum TracingTypes

        General

        Performance

        WebRequest

        Schema

        DatabaseAccess

        SQL

        SystemAccess

        Email

    End Enum
 

    Public Class Tracing
 

#Region " Members "
 

        Private Shared mobjGeneralSwitch As New TraceSwitch("General", "General Tracing")

        Private Shared mobjPerformanceSwitch As New TraceSwitch("Performance", "Performance Tracing")

        Private Shared mobjWebRequestSwitch As New TraceSwitch("WebRequest", "Web Request Tracing")

        Private Shared mobjSchemaSwitch As New TraceSwitch("Schema", "Schema Tracing")

        Private Shared mobjDatabaseAccessSwitch As New TraceSwitch("DatabaseAccess", "Database Access Tracing")

        Private Shared mobjSQLSwitch As New TraceSwitch("SQL", "SQL Tracing")

        Private Shared mobjCoreSwitch As New TraceSwitch("SystemAccess", "SystemAccess Tracing")

        Private Shared mobjEmailSwitch As New TraceSwitch("Email", "Email Tracing")
 

#End Region
 

#Region " Methods "
 

        Private Shared Function GetSwitch(ByVal pobjType As TracingTypes) As TraceSwitch

            Select Case pobjType

                Case TracingTypes.General

                    Return mobjGeneralSwitch

                Case TracingTypes.Performance

                    Return mobjPerformanceSwitch

                Case TracingTypes.WebRequest

                    Return mobjWebRequestSwitch

                Case TracingTypes.Schema

                    Return mobjSchemaSwitch

                Case TracingTypes.DatabaseAccess

                    Return mobjDatabaseAccessSwitch

                Case TracingTypes.SQL

                    Return mobjSQLSwitch

                Case TracingTypes.SystemAccess

                    Return mobjCoreSwitch

                Case TracingTypes.Email

                    Return mobjEmailSwitch

                Case Else

                    Return mobjGeneralSwitch

            End Select

        End Function
 

        Public Shared Sub [Error](ByVal pobjType As TracingTypes, ByVal pobjException As Exception)

            If GetSwitch(pobjType).TraceError Then

                Dim lobjCallingMethod As New CalledFromMethod

                Write(pobjType, lobjCallingMethod.ClassName, lobjCallingMethod.MethodName, "", pobjException)

                'Write(pobjType, pobjException)

            End If

        End Sub

        '

        'DSmith 24/05/06 - Added new Error tracing method that allows you to pass in a description

        '

        Public Shared Sub [Error](ByVal pobjType As TracingTypes, ByVal pstrDescription As String, ByVal pobjException As Exception)

            If GetSwitch(pobjType).TraceError Then

                Dim lobjCallingMethod As New CalledFromMethod

                Write(pobjType, lobjCallingMethod.ClassName, lobjCallingMethod.MethodName, pstrDescription, pobjException)

                'Write(pobjType, pobjException)

            End If

        End Sub
 

        Public Shared Sub Warning(ByVal pobjType As TracingTypes, ByVal pstrMessage As String)

            If GetSwitch(pobjType).TraceWarning Then

                Dim lobjCallingMethod As New CalledFromMethod

                Write(pobjType, "WARNING", lobjCallingMethod.ClassName, lobjCallingMethod.MethodName, pstrMessage)

                'Write(pobjType, "WARNING", pstrMessage)

                'Write(pobjType, UtilityClasses.Resources.SPFResources.GetString(6499), pstrMessage)                                 ' 6499 = WARNING

            End If

        End Sub
 

        Public Shared Sub Info(ByVal pobjType As TracingTypes, ByVal pstrMessage As String)

            If GetSwitch(pobjType).TraceInfo Then

                Dim lobjCallingMethod As New CalledFromMethod

                Write(pobjType, "INFO", lobjCallingMethod.ClassName, lobjCallingMethod.MethodName, pstrMessage)

                'Write(pobjType, "INFO", pstrMessage)

                'Write(pobjType, UtilityClasses.Resources.SPFResources.GetString(6500), pstrMessage)                                 ' 6500 = INFO

            End If

        End Sub
 

        Public Shared Sub Verbose(ByVal pobjType As TracingTypes, ByVal pstrMessage As String)

            If GetSwitch(pobjType).TraceVerbose Then

                Dim lobjCallingMethod As New CalledFromMethod

                Write(pobjType, "VERBOSE", lobjCallingMethod.ClassName, lobjCallingMethod.MethodName, pstrMessage)

                'Write(pobjType, "VERBOSE", pstrMessage)

                'Write(pobjType, UtilityClasses.Resources.SPFResources.GetString(6501), pstrMessage)                                 ' 6501 = VERBOSE

            End If

        End Sub
 

        Public Shared Sub Verbose(ByVal pobjType As TracingTypes, _

                    ByVal pstrMessage As String, _

                    ByVal pstrTimeSpan As String)

            If GetSwitch(pobjType).TraceVerbose Then

                Dim lobjCallingMethod As New CalledFromMethod

                Write(pobjType, "VERBOSE", lobjCallingMethod.ClassName, lobjCallingMethod.MethodName, pstrMessage, pstrTimeSpan)

                '   Write(pobjType, UtilityClasses.Resources.SPFResources.GetString(6501), pstrClassName, pstrMethodName, pstrMessage, pstrTimeSpan) ' 6501 = VERBOSE

            End If

        End Sub
 

#Region " Write Methods "
 

        Private Shared Sub Write(ByVal pobjType As TracingTypes, _

                                    ByVal pstrClassName As String, _

                                    ByVal pstrMethodName As String, _

                                    ByVal pstrDescription As String, _

                                    ByVal pobjException As Exception)

            Dim lobjException As Exception = pobjException
 
 

            If pstrDescription <> "" Then

                Write(pobjType, "EXCEPTION", pstrClassName, pstrMethodName, pstrDescription)

            End If
 

            While (Not lobjException Is Nothing)

                Dim lstrMsg As String = lobjException.Message

                Dim lstrStack As String = lobjException.StackTrace
 

                If lstrMsg.Length > 0 Then

                    Write(pobjType, "EXCEPTION", pstrClassName, pstrMethodName, lstrMsg)      ' 6502 = EXCEPTION

                    'Write(pobjType, UtilityClasses.Resources.SPFResources.GetString(6502), pstrClassName, pstrMethodName, lstrMsg)      ' 6502 = EXCEPTION

                End If

                If Not lstrStack Is Nothing Then

                    Write(pobjType, "EXCEPTION", pstrClassName, pstrMethodName, lstrStack)    ' 6502 = EXCEPTION

                    'Write(pobjType, UtilityClasses.Resources.SPFResources.GetString(6502), pstrClassName, pstrMethodName, lstrStack)    ' 6502 = EXCEPTION

                End If
 

                lobjException = lobjException.InnerException

            End While

        End Sub
 

        Private Shared Sub Write(ByVal pobjType As TracingTypes, _

                                    ByVal pstrCategory As String, _

                                    ByVal pstrClassName As String, _

                                    ByVal pstrMethodName As String, _

                                    ByVal pstrMessage As String)

            Write(pobjType, pstrCategory, pstrClassName, pstrMethodName, pstrMessage, Nothing)

        End Sub
 

        Private Shared Sub Write(ByVal pobjType As TracingTypes, _

                                    ByVal pstrCategory As String, _

                                    ByVal pstrClassName As String, _

                                    ByVal pstrMethodName As String, _

                                    ByVal pstrMessage As String, _

                                    ByVal pstrTimeSpan As String)

            Dim lobjValue As New System.Text.StringBuilder
 

            lobjValue.Append(pstrCategory)

            lobjValue.Append("|")

            lobjValue.Append(pobjType.ToString)

            lobjValue.Append("|")

            'lobjValue.Append(CType(AppDomain.GetCurrentThreadId, String))

            lobjValue.Append(CType(System.Threading.Thread.CurrentThread.ManagedThreadId, String))

            lobjValue.Append("|")

            If pstrClassName Is Nothing Then

                lobjValue.Append(" ")

            Else

                lobjValue.Append(pstrClassName)

            End If

            lobjValue.Append("|")

            If pstrMethodName Is Nothing Then

                lobjValue.Append(" ")

            Else

                lobjValue.Append(pstrMethodName)

            End If

            lobjValue.Append("|")

            lobjValue.Append(Date.Now.ToString("yyyy/MM/dd-HH:mm:ss:fff"))

            lobjValue.Append("|")

            lobjValue.Append(pstrMessage)

            If Not pstrTimeSpan Is Nothing Then

                lobjValue.Append("|")

                lobjValue.Append(pstrTimeSpan)

            End If

            'Trace.WriteLine(lobjValue.ToString)

            If pobjType = TracingTypes.Email Then

                If Trace.Listeners.Item("EmailfileOut") IsNot Nothing Then

                    Trace.Listeners.Item("EmailfileOut").WriteLine(lobjValue.ToString)

                End If

            Else

                If Trace.Listeners.Item("fileOut") IsNot Nothing Then

                    Trace.Listeners.Item("fileOut").WriteLine(lobjValue.ToString)

                End If

            End If

        End Sub
 
 

#End Region
 

#End Region
 

#Region " Private CalledFromMethod Class "
 

        Public Class CalledFromMethod
 

            Private mstrClassName As String = ""

            Private mstrMethodName As String = ""
 

            Public Sub New()

                Dim lobjTrace As Global.System.Diagnostics.StackTrace = New Global.System.Diagnostics.StackTrace()
 

                Dim lobjCallingFrame As Global.System.Diagnostics.StackFrame

                If lobjTrace.GetFrame(1).GetMethod.Name <> "DebugPrint" Then

                    '

                    ' We know that lobjTrace.GetFrame(1) is the method that called this and 0 is this method method 

                    ' so we need to extract the previous one.

                    '

                    lobjCallingFrame = lobjTrace.GetFrame(1)

                Else

                    '

                    ' If this is called from WriteToDebug then we have to extract the

                    ' method that called WriteToDebug

                    '

                    lobjCallingFrame = lobjTrace.GetFrame(2)

                End If
 

                mstrClassName = lobjCallingFrame.GetMethod.ReflectedType.FullName

                mstrMethodName = lobjCallingFrame.GetMethod.Name

            End Sub
 

            Public ReadOnly Property ClassName() As String

                Get

                    Return mstrClassName

                End Get

            End Property
 

            Public ReadOnly Property MethodName() As String

                Get

                    Return mstrMethodName

                End Get

            End Property
 

        End Class
 

#End Region
 

Private Shared mobjLogFile As IO.StreamWriter

        Public Shared Sub OpenLog()

            mobjLogFile = IO.File.AppendText(LogFilePath)

            mobjLogFile.AutoFlush = True

        End Sub
 

        Public Shared Sub CloseLog()

            If Not mobjLogFile Is Nothing Then
 

                'mobjLogFile.Flush()

                mobjLogFile.Close()

            End If

        End Sub

        Public Shared ReadOnly Property LogFilePath() As String

            Get

                Return IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "MyLog.txt")

            End Get

        End Property

        Public Shared Sub DebugPrint(ByVal pstrMessage As String)

            If Not mobjLogFile Is Nothing Then

                Try

                    Dim lobjCallingMethod As New SPOClient.Tracing.CalledFromMethod

                    mobjLogFile.WriteLine(lobjCallingMethod.ClassName & "|" & lobjCallingMethod.MethodName & "|" & pstrMessage)

                Catch ex As Exception
 

                End Try

            End If

        End Sub
 
 

    End Class
 

    ' TODO: Write to the event log

    Public Class EventLog
 

    End Class
 

End Namespace

Open in new window

0
 
LVL 6

Expert Comment

by:cottsak
ID: 21862039
how much logging do you need? how big is your application?
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now