Ignore Current method on Log4net

I have a pattern layout set as:

<param name="ConversionPattern" value="[%-5level]%date %class.%M - %message%newline" />

Which allows me to log the class and the method name of the current log execution.

However, I created a static method to perform some logic for the ILog method, so that, locally I can log as Info and on other environments as Debug level

 [StringFormatMethod("msg")]
  public static void DebugOrInfoFormat(this ILog log, string msg, params object[] parameters) {
        if (ApplicationConfiguration.IsLocal()) {
            log.InfoFormat(msg, parameters);
        } else {
            log.DebugFormat(msg, parameters);
        }
    }

Thing is, like that, on the output I´d get DebugOrInfoFormat instead of the callee method that called it, making it confusing since this method is intended to be used across many places.

Question is:

Is there ant attribute that can be placed on that method so that the log4net ignores that method on the stacktrace?

Tried already, with no luck

    [System.Diagnostics.DebuggerHidden]
    [System.Diagnostics.DebuggerNonUserCode]

Thanks

Upvotes: 1

Views: 216

Answers (1)

FortyTwo
FortyTwo

Reputation: 2639

An alternate approach would be to use StackTrace and obtain the calling method and then custom create the logging message. Something like this:

StackTrace st = new StackTrace(true);
StackFrame sf = st.GetFrame(1);
//build your message
string newMessage = string.Format("{0} at {1} in {2}:line {3}", msg, sf.GetMethod().ToString(), sf.GetFileName(), sf.GetFileLineNumber());
if (ApplicationConfiguration.IsLocal()) 
{
  _logger.Info(newMessage);
} 
else
{
  _logger.Debug(newMessage);
}

where _logger is declared as:

private static ILog _logger = LogManager.GetLogger(typeof(Logger));

Upvotes: 1

Related Questions