-2

I'm trying to do system log and I keep getting this error. The logger object is Clearly instialized.

Object reference not set to an instance of an object. Source File: C:\Users\Victor\DEV\chams\repos-development\confirm-me-api-1\Utility\AuditLogAttribute.cs. Line: 65

using ConfirmMeAPI.Models;
using Newtonsoft.Json;
using System;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Mvc;
using ActionFilterAttribute = System.Web.Mvc.ActionFilterAttribute;

namespace ConfirmMeAPI.Utility
{
    public class AuditLogAttribute : ActionFilterAttribute
    {
        cfmWS.WebService _cfm = new cfmWS.WebService();
        GeolocationResponse _geolocation = new GeolocationResponse();
        Logger _logger = new Logger();
        private readonly HttpClient _httpClient;
        string baseurl;
        public AuditLogAttribute()
        {
            baseurl = "http://ip-api.com";
            _httpClient = new HttpClient();
            _httpClient.BaseAddress = new Uri(baseurl);
            _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        }
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            try
            {
                ...
            }
            catch (Exception ex)
            {
                /* THE ERROR LINE */ _logger.LogError(ex.ToString() + ex.InnerException.ToString() + ex.StackTrace.ToString());

            }
        }

    }

}

kachi_dk
  • 211
  • 2
  • 5

2 Answers2

2

It is most likely .InnerException that will be null. Not all exceptions have one.

You should only need to log ex.ToString() and the InnerException and StackTrace will be logged as part of that anyway.

And depending on which logger it is, you may be OK just logged ex and it will handle the rest.

DaveShaw
  • 52,123
  • 16
  • 112
  • 141
0

Object reference related errors are run time errors, which normally comes when you try to refer a variables or any data items returning value null. Make sure you have initialized that variable of data items.