How to get the Stack trace when logging exceptions with NLog?

后端 未结 4 704
甜味超标
甜味超标 2020-12-23 15:57

When I use the default layout with NLog it only prints the name of the exception. I\'ve been told that the log4jxmlevent layout doesn\'t prints nothing about the exception.

4条回答
  •  [愿得一人]
    2020-12-23 16:52

    Use the overloads that take an Exception as the second argument:

    catch(Exception crap)
    {
        log.Error("Something went horribly wrong.", crap);
    }
    

    Then in your layout include the ${exception} layout renderer:

    
    

    Sources:

    • https://github.com/NLog/NLog/wiki/How-to-Log-Exceptions
    • https://github.com/nlog/NLog/wiki/Exception-Layout-Renderer

提交回复
热议问题