1

I have written a Singleton wrapper for log4net and it will be called across all layers of my WCF service. I am unable to log calling method name. Pls suggest if any inputs from my below code. Thanks.

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Imports log4net
Imports System.Reflection

Public Class Logger
Implements ILog

Private Shared m_instance As Logger
Private Shared log As log4net.ILog = Nothing

Public Sub New()
    If log Is Nothing Then
        'log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType) ' -> This always logs 'Logger'
        log = log4net.LogManager.GetLogger(Assembly.GetCallingAssembly(), "MyLogger")  ' -> This always logs 'MyLogger'
    End If

End Sub

Public Shared ReadOnly Property Write() As Logger
    Get
        m_instance = New Logger()
        Return m_instance
    End Get
End Property

Public Sub Debug(msg As String) Implements ILog.Debug
    log.Debug(msg)
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''  

Public Interface ILog
     Sub Debug(msg As String)
End Interface
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

I have made it more simple as below to log method name. But I have to pass classname as parameter. Pls suggest is this acceptable design?

''''' Calling code ''''''''''''''''''''
Logger(of MyClassName).Debug("message")


''''''Log4net wrapper'''''''''''''''''''''
Imports log4net
Imports System.Reflection

Public NotInheritable Class Logger(Of T)

Private Shared log As log4net.ILog = Nothing

Private Shared ReadOnly Property LogProvider() As log4net.ILog
    Get
        If (log Is Nothing) Then
            Dim CallingMethod As String = GetType(T).ToString() & "." & (New StackTrace()).GetFrame(2).GetMethod().Name
            log = log4net.LogManager.GetLogger(CallingMethod)
        End If
        Return log
    End Get
End Property

Public Shared Sub Debug(msg As String)
    LogProvider.Debug(msg)
End Sub

1 Answer 1

1

A logging instance can only have one name, you can't change it. If you want to use a diffrent name in each method you need to make a logger for ech method.

    log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType) ' -> This always logs 'Logger'
    log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().Name) ' -> This gives you the name of the current method
    log = log4net.LogManager.GetLogger(Assembly.GetCallingAssembly(), "MyLogger")  ' -> This always logs 'MyLogger'

Creating the logger inside your method like:

    ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().Name) ' -> This gives you the name of the current method

You will have a logger with the name of the method, I would add the name of the class either:

    ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType + "." + MethodBase.GetCurrentMethod().Name) ' -> This gives you the name of the current method
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.