microsoft / ApplicationInsights-aspnetcore

ASP.NET Core web applications monitoring
313 stars 123 forks source link

System.NullReferenceException if UseApplicationInsights was not added if used with Microsoft.Extensions.Logging #382

Closed MichaCo closed 7 years ago

MichaCo commented 7 years ago

The exception happens when using AI together with Microsoft.Extensions.Logging.

System.AggregateException occurred
  HResult=0x80131500
  Message=An error occurred while writing to logger(s).
  Source=<Cannot evaluate the exception source>
  StackTrace:
   at Microsoft.Extensions.Logging.Logger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func`3 formatter)
   at Microsoft.Extensions.Logging.Logger`1.Microsoft.Extensions.Logging.ILogger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func`3 formatter)
   at Microsoft.Extensions.Logging.LoggerExtensions.LogWarning(ILogger logger, String message, Object[] args)
...

Inner Exception 1:
NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.ApplicationInsights.AspNetCore.Logging.ApplicationInsightsLogger.IsEnabled(LogLevel logLevel)
   at Microsoft.ApplicationInsights.AspNetCore.Logging.ApplicationInsightsLogger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func`3 formatter)
   at Microsoft.Extensions.Logging.Logger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func`3 formatter)

I did not set up AI correctly by not having the UseApplicationInsights on the webhost builder

    new WebHostBuilder()
            // => without that one .UseApplicationInsights()

and later on in my Startup.cs I configure to have AI on the logger factory, too:

    loggerFactory.AddApplicationInsights(serviceProvider);

Now, if you try to log any message with _logger.LogWarning(...) for example, you'll get a NullReferenceException.

Why would I not configure the WebHostBuilder correctly you may ask, well, I encountered that while trying to get the Microsoft.AspNetCore.TestHost.TestServer running for unit testing my web app. While doing unit tests, I do not want to trigger AI events.

Also, In general, NullReferenceException is a little bit rude ;)

Expected behavior The expected behavior would be, that it just doesn't do anything. Logging should never throw any unexpected exceptions at runtime

dnduffy commented 7 years ago

https://github.com/Microsoft/ApplicationInsights-aspnetcore/pull/386

melborp commented 5 years ago

I dont know if this one has creeped back or not, but i am getting in one of my environments with asp.net core 2.2 (SDK 2.2.100, target netcoreapp2.2, Microsoft.ApplicationInsights.AspNetCore 2.6.1). Running in Azure App Service using the run from zip package option. Runs perfectly outside of zip package. System.AggregateException: An error occurred while writing to logger(s). (Object reference not set to an instance of an object.) ---> System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.ApplicationInsights.AspNetCore.Logging.ApplicationInsightsLogger.<>c__DisplayClass9_01.b0(DictionaryEntry item) at System.Collections.Generic.List1.ForEach(Action1 action) at Microsoft.ApplicationInsights.AspNetCore.Logging.ApplicationInsightsLogger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func3 formatter) at Microsoft.Extensions.Logging.Logger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func3 formatter) --- End of inner exception stack trace --- at Microsoft.Extensions.Logging.Logger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func3 formatter) at Microsoft.Extensions.Logging.Logger1.Microsoft.Extensions.Logging.ILogger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func`3 formatter) at Microsoft.Extensions.Logging.LoggerMessage.<>cDisplayClass4_0.b0(ILogger logger, Exception exception) at Microsoft.AspNetCore.Diagnostics.Internal.DiagnosticsLoggerExtensions.UnhandledException(ILogger logger, Exception exception) at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.Invoke(HttpContext context) at NWebsec.AspNetCore.Middleware.Middleware.MiddlewareBase.Invoke(HttpContext context) at NWebsec.AspNetCore.Middleware.Middleware.MiddlewareBase.Invoke(HttpContext context) at Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Server.IISIntegration.IISMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application) ---> (Inner Exception #0) System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.ApplicationInsights.AspNetCore.Logging.ApplicationInsightsLogger.<>cDisplayClass9_01.<Log>b__0(DictionaryEntry item) at System.Collections.Generic.List1.ForEach(Action1 action) at Microsoft.ApplicationInsights.AspNetCore.Logging.ApplicationInsightsLogger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func3 formatter) at Microsoft.Extensions.Logging.Logger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func3 formatter)<---

Sandeep321 commented 5 years ago

I am using below:

Still getting below error sometimes:

(Inner Exception #0) System.NullReferenceException: Object reference not set to an instance of an object.
         at Microsoft.ApplicationInsights.AspNetCore.Logging.ApplicationInsightsLogger.<>c__DisplayClass9_0`1.<Log>b__0(DictionaryEntry item)
         at System.Collections.Generic.List`1.ForEach(Action`1 action)
         at Microsoft.ApplicationInsights.AspNetCore.Logging.ApplicationInsightsLogger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func`3 formatter)
         at Microsoft.Extensions.Logging.Logger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func`3 formatter)<---

I thought it was fixed in version 2.4 as mentioned in https://social.msdn.microsoft.com/Forums/aspnet/en-US/e8ca1a8c-d5f2-4c90-945e-3325c0aa771a/random-loginfo-exceptions?forum=AzureFunctions

Sandeep321 commented 5 years ago

Agreeing with @melborp