1

I have a log4net wrapper for logging error messages in web api application. It is working fine but i am not able to log calling method name. It only display's top level method name. Lets say, i have a method a of class A which calls method b of a class B and b logs error message. Log4net only displays Class A and method a but i want to display either full calling chain A-a-B-b or at least B-b

private static readonly ILog LoggerObject = LogManager.GetLogger("ErrorLog");

log4net config

<log4net>
    <appender name="ErrorLog" type="log4net.Appender.RollingFileAppender">
        <file value="LogBackUp2.log" />
        <staticLogFileName value="false" />
        <appendToFile value="true" />
        <rollingStyle value="Date" />
        <datePattern value="yyyy-MM-dd.'Err'" />
        <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%M %C] - %message%newline" />
        </layout>
    </appender>
    <logger name="ErrorLog">
        <maximumFileSize value="15MB" />
        <appender-ref ref="ErrorLog" />
    </logger>
</log4net>

If i use this then log4net doesn't create any log file.

LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
pfx
  • 20,323
  • 43
  • 37
  • 57
user1263981
  • 2,953
  • 8
  • 57
  • 98
  • I think 'GetCurrentMethod()' will give the wrong result if the method will be inlined due to compiler optimizations. Perhaps you can use the CallerMemberName attribute. – Silvermind Nov 17 '18 at 23:36
  • See [this](https://stackoverflow.com/questions/9319810/how-to-log-stack-trace-using-log4net-c) thread. – CodingYoshi Nov 17 '18 at 23:39

1 Answers1

0

use %stacktrace{5} to show 5 levels of the stacktrace which lead to the call of the log method. Replace the 5 with the level you want to have. example:

<conversionPattern value="%date [%thread] %-5level %logger [%stacktrace{5}] - %message%newline" />

When you use a log4net wrapper you may want to apply this to get rid of the wrapper in the stacktrace

Welcor
  • 2,431
  • 21
  • 32