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.

1

There are 1 answers

0
Nripendra Ojha On

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.