Developer forum

Forum » CMS - Standard features » Logger issue
Scott Forsyth Dynamicweb Employee
Scott Forsyth
Reply

Hello,

I have some code that is trying to log when an error occurs. It only happens in production so I can't easily reproduce it so I'm trying to catch it in the log. I have the following code:

      catch (Exception ex)
      {
        var ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        Dynamicweb.Logging.LogManager.Current.GetLogger(LogFolder).Error($"Failed creating switchaccount cookie. UserId: {itemUser.ID}. IP Address: {ip}. {ex.Message}");
      }

Note that I'm doing a null check for itemUser prior to this code, so I know that it's not null. And LogFolder is a valid string.

However, the log entry is just this:

2022-03-12 00:45:04.7199|INFO|DynamicwebNA/AutoImpersonate|Object reference not set to an instance of an object.

Notice that it's an INFO log and not an Error log, and it doesn't output the message that I intended. 

What do I have wrong in the syntax for the logger?


Replies

 
Nicolai Pedersen
Reply

Hio Scott

My guess is that HttpContext.Current.Request is null. It can be a callback. a schedule task, an email or something else where the http context is not present.

Also just to be sure change  {itemUser.ID} to  {itemUser?.ID}

 
Scott Forsyth Dynamicweb Employee
Scott Forsyth
Reply

Hi Nicolai,

Good thought. I'll pursue that option. And I'll add the null check there too, just to be sure.

Thanks,

Scott

 

You must be logged in to post in the forum