We must all be familiar with the log4net of the top of the name. In the project application, Log4net is often packaged two times to meet the special needs of the project. For example, the automatic logging call Method name of the log method, the class name.
Use System.Diagnostics.StackTrace to see which method calls the method (and which method calls its caller, and so on). As you can see from its name, the StackTrace object tracks all the hanging processes that are waiting for the current procedure to complete. You can create StackTrace objects in many ways. In its simplest form, the argument is not passed to its constructor, and a complete stack image as a set of StackFrame objects can be enumerated by its index. Dim St as New StackTrace ()
' Enumerate all the stack frame objects.
' The frame at index 0 corresponds to the current routine.
For I as an Integer = 0 to St. FrameCount-1
' Get the ith stack frame and print the ' method name.
Dim SF as StackFrame = St. GetFrame (i)
Console.WriteLine (SF. GetMethod (). Name)
Next
Then customize a Log4net wrapper class, using StackTrace to get Log4net wrapper class name and invocation method name: Imports log4net
Imports System.Runtime.CompilerServices
Imports System.Diagnostics
public class Logger Class Logger
Private Sub New () Sub New ()
End Sub
Private Shared log as ILog = Logmanager.getlogger (System.Reflection.MethodBase.GetCurrentMethod (). DeclaringType)
Shared Sub New () Sub New ()
Loggerconfig.config ()
End Sub
<methodimpl (methodimploptions.noinlining) > _
Private Shared function FormatMessage () function FormatMessage (ByVal message as String)
Dim St as StackTrace = New StackTrace ()
Dim SF as StackFrame = St. GetFrame (2)
Return String.Format ("{0}.{ 1}: {2} ", SF. GetMethod (). Declaringtype.name, _
sf. GetMethod (). Name, message)
End Function
Public Shared Sub logdebug () Sub Logdebug (ByVal message as String)
If log. Isdebugenabled Then
Log. Debug (formatmessage (message))
End If
End Sub
Public Shared Sub loginfo () Sub Loginfo (ByVal message as String)
If log. Isinfoenabled Then
Log. Info (FormatMessage)
End If
End Sub
Public Shared Sub Logwarn () Sub Logwarn (ByVal message as String)
If log. Iswarnenabled Then
Log. Warn (formatmessage (message))
End If
End Sub
Public Shared Sub logerror () Sub Logerror (ByVal message as String)
If log. Iserrorenabled Then
Log. Error (formatmessage (message))
End If
End Sub
Public Shared Sub logfatal () Sub Logfatal (ByVal message as String)
If log. Isfatalenabled Then
Log. Fatal (formatmessage (message))
End If
End Sub
End Class
Example: Public