3

I am trying to upgrade the .netcore framework from 2.2 to 3.1 . But I want to use IHostingEnvironment, IWebHostBuilder and AddNewtonsoftJson in 3.1 also. (I know they should be replaced but if I will do so, code refactoring will be required, which i don't need right now).

When I am executing an api using swagger then getting 500 error with message:

can't parse JSON. Raw result: System.Exception: Method not found: 'Microsoft.Extensions.Logging.ILoggerFactory Microsoft.Extensions.Logging.ConsoleLoggerExtensions.AddConsole(Microsoft.Extensions.Logging.ILoggerFactory, Microsoft.Extensions.Logging.LogLevel)'.

And this is the beginning of stack trace

fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1] An unhandled exception has occurred while executing the request. System.MissingMethodException: Method not found: 'Microsoft.Extensions.Logging.ILoggerFactory Microsoft.Extensions.Logging.ConsoleLoggerExtensions.AddConsole(Microsoft.Extensions.Logging.ILoggerFactory, Microsoft.Extensions.Logging.LogLevel)'.

I am configuring Logger only in Program.cs .

 public static IWebHostBuilder CreateWebHostBuilder (string[] args) =>
            WebHost.CreateDefaultBuilder (args)
            .UseUrls ("http://0.0.0.0:5000")
            .ConfigureLogging ((hostingContext, logging) => {
                logging.AddConfiguration (hostingContext.Configuration.GetSection ("Logging"));
                logging.AddConsole ();
                logging.AddApplicationInsights ();
            })
            .UseStartup<Startup> ();
    }

Configurations in appsettings.json

"Logging": {
        "IncludeScopes": false,
        "ApplicationInsights": {
            "LogLevel": {
                "Default": "Warning",
                "Microsoft": "Error"
            }
        },
        "LogLevel": {
            "Default": "Information",
            "System": "Warning",
            "Microsoft": "Warning"
        },
        "Console": {
            "IncludeScopes": true
        }
    }

I am not understanding why it's not working.

Note: I have 6 APIs in this project and 1 is working successfully with the above changes and 5 are failing with the above error.

  • You should provide us where the logger is added most probably the program.cs... and I am pretty sure you need to add/update this nuget `Microsoft.Extensions.Logging.Console` – panoskarajohn Jun 07 '21 at 12:56
  • I am injecting ILoggerFactory in Startup.cs and using that Logger parameter in ConfigureServices to call another function as 'services.ConfigureServicesDependencies (Configuration, _logger);' – kamini kumari Jun 07 '21 at 13:01
  • 1
    You should provide us that code, pls update your question with where your logger is added in the app as well as your config. Also please try adding the nugget i suggested, it might not fix but some different error should appear. – panoskarajohn Jun 07 '21 at 13:03
  • I tried add but it didn't worked for me. – kamini kumari Jun 07 '21 at 13:05
  • You should try and figure out why... you might be missing other dependencies. The thing is the error you are getting, the method is in that nuget. Either search you code and remove the `AddConsole()` or add this package – panoskarajohn Jun 07 '21 at 13:07
  • @panoskarajohn I updated my question, can you please look into it. – kamini kumari Jun 07 '21 at 13:12
  • Try removing the `logging.AddConsole ();`. Also please add from your configuration `appsetting.json` or whatever env you are running .json and append it as well – panoskarajohn Jun 07 '21 at 13:17
  • Tried removing logging.AddConsole ();, but same error is reflecting. – kamini kumari Jun 07 '21 at 13:34
  • Hmm i see then, it could be that it is picked up by the configuration. Could you temporarily remove Console from the settings as well? – panoskarajohn Jun 07 '21 at 13:39
  • Tried, but no success. – kamini kumari Jun 07 '21 at 13:54
  • 1
    There's nothing wrong with your logging package, further in the callstack (what's left out) it should show which component in your stack is calling this old APIs (that were removed). – davidfowl Jun 07 '21 at 17:46
  • at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass5_0.g__CreateController|0(ControllerContext controllerContext) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync() --- End of stack trace from previous location where exception was thrown --- Stack Trace – kamini kumari Jun 08 '21 at 05:57
  • 1
    Can you share the application's .csproj file content? so that we can know the installed package version. And from the Stack trace, it seems that your application is an Asp.net Core MVC application, right? – Zhi Lv Jun 08 '21 at 06:09
  • @ZhiLv yes it is MVC application – kamini kumari Jun 08 '21 at 07:28
  • have you tried upgrading to 6.0 RC1? This resolved the issue for me – ossentoo Sep 16 '21 at 10:25

1 Answers1

0

As I read in discussion John has given correct suggestion. In visual Studio go to NuGet - Solution and update all the package which has require to update.

Nripendra Ojha
  • 379
  • 2
  • 14