Thursday, March 1, 2018

Logging MethodBase.GetCurrentMethod().Name Results in "MoveNext"

Using log4net, the following code example has always (that I've paid attention to) successfully written the method name to the log:

CODE:
catch (Exception ex)
{
  globalMgr.Logger.Debug($"Exception thrown in  {MethodBase.GetCurrentMethod().Name} {ex.Message}  {ex.StackTrace} ");
  return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);


LOG TEXT:
Starting HttpClient in GetStoreData()

Lately, however, the method base is logged as "MoveNext."

LOG TEXT:
Starting HttpClient in MoveNext

The reason for this is that I'm placing the GetCurrentMethod call inside an async method.   Therefore, the final method that the code lives is indeed in "MoveNext." 

If I don't get back with a solution soon enough, hard-code your method names into the logging but do not tell anyone I told you to do such a thing.

No comments:

Post a Comment