简体   繁体   中英

logging exception in .net core worker process

i have below code in .net core worker process.

public void GenerateException()
{
    try
    {
        int i = 10;
        int j = 0;
        int k = i / j;
    }
    catch (Exception ex)
    {
        _logger.LogError("Exception at GenerateException", ex);
    }
}

I am using https://serilog.net/ as logging framework. The issue is this does not log exception. it only logs below line but not actual exception

2021-11-12 11:52:34.359 +05:30 [ERR] Exception at GenerateException

What am i doing wrong?

With some googling found that i had to swap the parameters in _logger.LogError like _logger.LogError(ex,"Exception at GenerateException") ;

Check official site . This page mentioned that you should write like this:

_logger.LogError("Exception at GenerateException {Exception}", ex);

ex will be formatted into the position of {Exception}.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM