Using Stacktrack to increase log4net output information

Source: Internet
Author: User
Tags log4net

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.