44using System . Diagnostics ;
55using System . Reflection ;
66
7- namespace jjm . one . Serilog . Extensions . Logging . Helpers
7+ namespace jjm . one . Serilog . Extensions . Logging . Helpers ;
8+
9+ /// <summary>
10+ /// Static class for all function logging helper extensions.
11+ /// </summary>
12+ public static class FunctionLogging
813{
14+
915 /// <summary>
10- /// Static class for all function logging helper extensions .
16+ /// Log a function/method call .
1117 /// </summary>
12- public static class FunctionLogging
18+ /// <param name="logger">The logger instance.</param>
19+ /// <param name="level">The logging level. Default is <see cref="LogEventLevel.Debug"/>.</param>
20+ public static void LogFctCall ( this ILogger logger , LogEventLevel level = LogEventLevel . Debug )
1321 {
22+ var method = new StackTrace ( ) . GetFrame ( 1 ) ? . GetMethod ( ) ;
1423
15- /// <summary>
16- /// Log a function/method call.
17- /// </summary>
18- /// <param name="logger">The logger instance.</param>
19- /// <param name="level">The logging level. Default is <see cref="LogEventLevel.Debug"/>.</param>
20- public static void LogFctCall ( this ILogger logger , LogEventLevel level = LogEventLevel . Debug )
21- {
22- var method = new StackTrace ( ) . GetFrame ( 1 ) ? . GetMethod ( ) ;
23-
24- logger . Write ( level , "Function called: {ClassName} -> {FctName}" ,
25- method ? . DeclaringType ? . Name , method ? . Name ) ;
26- }
24+ logger . Write ( level , "Function called: {ClassName} -> {FctName}" ,
25+ method ? . DeclaringType ? . Name , method ? . Name ) ;
26+ }
2727
28- /// <summary>
29- /// Log a function/method call.
30- /// </summary>
31- /// <param name="logger">The logger instance.</param>
32- /// <param name="classType">The <see cref="Type"/> of the calling class.</param>
33- /// <param name="methodType">The <see cref="MethodBase"/> of the calling function/method.</param>
34- /// <param name="level">The logging level. Default is <see cref="LogEventLevel.Debug"/>.</param>
35- public static void LogFctCall ( this ILogger logger , Type ? classType , MethodBase ? methodType ,
36- LogEventLevel level = LogEventLevel . Debug )
28+ /// <summary>
29+ /// Log a function/method call.
30+ /// </summary>
31+ /// <param name="logger">The logger instance.</param>
32+ /// <param name="classType">The <see cref="Type"/> of the calling class.</param>
33+ /// <param name="methodType">The <see cref="MethodBase"/> of the calling function/method.</param>
34+ /// <param name="level">The logging level. Default is <see cref="LogEventLevel.Debug"/>.</param>
35+ public static void LogFctCall ( this ILogger logger , Type ? classType , MethodBase ? methodType ,
36+ LogEventLevel level = LogEventLevel . Debug )
37+ {
38+ logger . Write ( level , "Function called: {ClassName} -> {FctName}" ,
39+ classType ? . Name , methodType ? . Name ) ;
40+ }
41+
42+ /// <summary>
43+ /// Log an exception within a function/method call.
44+ /// </summary>
45+ /// <param name="logger">The logger instance.</param>
46+ /// <param name="exc">The exception.</param>
47+ /// <param name="msg">The message of the exception.</param>
48+ /// <param name="level">The logging level. Default is <see cref="LogEventLevel.Error"/>.</param>
49+ public static void LogExcInFctCall ( this ILogger logger , Exception exc , string ? msg = null ,
50+ LogEventLevel level = LogEventLevel . Error )
51+ {
52+ var method = new StackTrace ( ) . GetFrame ( 1 ) ? . GetMethod ( ) ;
53+
54+ if ( string . IsNullOrEmpty ( msg ) )
3755 {
38- logger . Write ( level , "Function called: {ClassName} -> {FctName}" ,
39- classType ? . Name , methodType ? . Name ) ;
56+ msg = string . Empty ;
4057 }
41-
42- /// <summary>
43- /// Log an exception within a function/method call.
44- /// </summary>
45- /// <param name="logger">The logger instance.</param>
46- /// <param name="exc">The exception.</param>
47- /// <param name="msg">The message of the exception.</param>
48- /// <param name="level">The logging level. Default is <see cref="LogEventLevel.Error"/>.</param>
49- public static void LogExcInFctCall ( this ILogger logger , Exception exc , string ? msg = null ,
50- LogEventLevel level = LogEventLevel . Error )
58+ else
5159 {
52- var method = new StackTrace ( ) . GetFrame ( 1 ) ? . GetMethod ( ) ;
53-
54- if ( string . IsNullOrEmpty ( msg ) )
55- {
56- msg = string . Empty ;
57- }
58- else
59- {
60- msg = "\n " + msg ;
61- }
62-
63- logger . Write ( level , exc , "Exception thrown in: {ClassName} -> {FctName}{CustomMsg}" ,
64- method ? . DeclaringType ? . Name , method ? . Name , msg ) ;
60+ msg = "\n " + msg ;
6561 }
62+
63+ logger . Write ( level , exc , "Exception thrown in: {ClassName} -> {FctName}{CustomMsg}" ,
64+ method ? . DeclaringType ? . Name , method ? . Name , msg ) ;
65+ }
6666
67- /// <summary>
68- /// Log an exception within a function/method call.
69- /// </summary>
70- /// <param name="logger">The logger instance.</param>
71- /// <param name="exc">The exception.</param>
72- /// <param name="classType">The <see cref="Type"/> of the calling class.</param>
73- /// <param name="methodType">The <see cref="MethodBase"/> of the calling function/method.</param>
74- /// <param name="msg">The message of the exception.</param>
75- /// <param name="level">The logging level. Default is <see cref="LogEventLevel.Error"/>.</param>
76- public static void LogExcInFctCall ( this ILogger logger , Exception exc , Type ? classType ,
77- MethodBase ? methodType , string ? msg = null , LogEventLevel level = LogEventLevel . Error )
67+ /// <summary>
68+ /// Log an exception within a function/method call.
69+ /// </summary>
70+ /// <param name="logger">The logger instance.</param>
71+ /// <param name="exc">The exception.</param>
72+ /// <param name="classType">The <see cref="Type"/> of the calling class.</param>
73+ /// <param name="methodType">The <see cref="MethodBase"/> of the calling function/method.</param>
74+ /// <param name="msg">The message of the exception.</param>
75+ /// <param name="level">The logging level. Default is <see cref="LogEventLevel.Error"/>.</param>
76+ public static void LogExcInFctCall ( this ILogger logger , Exception exc , Type ? classType ,
77+ MethodBase ? methodType , string ? msg = null , LogEventLevel level = LogEventLevel . Error )
78+ {
79+ if ( string . IsNullOrEmpty ( msg ) )
7880 {
79- if ( string . IsNullOrEmpty ( msg ) )
80- {
81- msg = string . Empty ;
82- }
83- else
84- {
85- msg = "\n " + msg ;
86- }
87-
88- logger . Write ( level , exc , "Exception thrown in: {ClassName} -> {FctName}{CustomMsg}" ,
89- classType ? . Name , methodType ? . Name , msg ) ;
81+ msg = string . Empty ;
9082 }
83+ else
84+ {
85+ msg = "\n " + msg ;
86+ }
87+
88+ logger . Write ( level , exc , "Exception thrown in: {ClassName} -> {FctName}{CustomMsg}" ,
89+ classType ? . Name , methodType ? . Name , msg ) ;
9190 }
92- }
91+ }
0 commit comments