StackTrace()
Enumerate all the stack frame objects.
The frame at index 0 corresponds to the current routine.
Integer
st.FrameCount
Get the ith stack frame and print the method name.
StackFrame
st.GetFrame(i)
Console.WriteLine(sf.GetMethod().Name)
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(message))
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
Class Form1
Class
Form1
Private
Sub Button1_Click()
Sub
Button1_Click(
ByVal
sender
As
System.Object,
ByVal
e
As
System.EventArgs)
Handles
Button1.Click
Logger.LogInfo(
"
Entering
"
)
CallTest()
Logger.LogInfo(
"
Exiting
"
)
End Sub
Private
Sub CallTest()
Sub
CallTest()
Logger.LogInfo(
"
Entering
"
)
Logger.LogInfo(
"
Exiting
"
)
End Sub
End Class
打印日志如下:
2007-02-25 20:38:27,000 [5692] INFO -
Form1.Button1_Click:
Entering
2007-02-25 20:38:27,015 [5692] INFO -
Form1.CallTest:
Entering
2007-02-25 20:38:27,015 [5692] INFO -
Form1.CallTest:
Exiting
2007-02-25 20:38:27,015 [5692] INFO -
Form1.Button1_Click:
Exiting