0

ASP.NET Core 5 Web API and in startup class, I use this method for global exception handling.

I want to log exception into the database.

    app.UseExceptionHandler(a => a.Run(async context =>
            {
                var exceptionHandlerPathFeature = context.Features.Get<IExceptionHandlerPathFeature>();
                var exception = exceptionHandlerPathFeature.Error;
                
                //  For Save Exception Log
                if(exception != null)
                {
                    exceptionLogger.AddException(exception);
                }

                // For Show Json Error in Resoponse
                await context.Response.WriteAsJsonAsync(new DTOResultObject(exception.Message));
            }));

my Exception logger class is:

 public  class ExceptionLogger : BaseService<ExceptionLog>, IExceptionService
    {

        protected IBaseRepository<ExceptionLog> _TRepository;
       
        public ExceptionLogger(IBaseRepository<ExceptionLog> TRepository) : base(TRepository)
        {
            _TRepository = (IBaseRepository<ExceptionLog>)TRepository;
        }
        

        public async Task addAddException(ExceptionLog exception)
        {
            //    mongo.insert<ExceptionLog>(document);
            await _TRepository.Insert(exception);
            await _TRepository.Save();
        }
        public async Task AddException(System.Exception exception1)
        {
            try
            {
                await addAddException(new ExceptionLog()
                {
                    exception = exception1,
                    date = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc),
                    message = exception1.Message,
                    stackTrace = exception1.StackTrace
                });
            }
            catch(Exception ex)
            {
               await addAddException(new ExceptionLog()
                {
                    exception = ex,
                    date = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc),
                    message = ex.Message,
                    stackTrace = ex.StackTrace
                });
            }
        }

and my exception log class :

 public class ExceptionLog : IEntity
    {
        //public ObjectId id { get; set; }
        //[JsonIgnore]
        public Exception exception { get; set; }
        public string message { get; set; }
        public string stackTrace { get; set; }
        public DateTime date { get; set; }
    }

I want to log exception into database, the exception is not null and it goes to the if() loop, but unfortunately it throws a null reference exception for the exception variable in the AddException method.

Could you please help me with this problem? Thanks a lot.

1 Answers1

0

Based on the code you provided, it seems that the issue might be with the exceptionLogger object or the AddException method itself. It's possible that the exceptionLogger object is null, or the AddException method is not handling the null exception parameter properly.

Hope it' Help

Dhiren Patel
  • 630
  • 8
  • 16