Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.MethodAccessException: 'Attempt by method 'Microsoft.Extensions.Logging.Configuration issue

In .NETCore, While running the application in Program.cs file at CreateWebHostBuilder(args).Build().Run();

I'm getting the Exception

"System.MethodAccessException: 'Attempt by method 'Microsoft.Extensions.Logging.Configuration.LoggerProviderConfigurationFactory.GetConfiguration(System.Type)' to access method 'Microsoft.Extensions.Logging.ProviderAliasUtilities.GetAlias(System.Type)' failed.'"

in the Method

 public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }

How to resolve this. I tried by uninstalling and Installing Microsoft.AspNetCore but no use, Please see the screenshot of my Dependenciesdependies i added in Project

Except theMicrosoft.Extension.Logging all the versions of dependcies are same. is there any problem that version difference. can someone help me to sort out this.

like image 733
chaitanya k Avatar asked Dec 19 '19 03:12

chaitanya k


People also ask

What is the use of Microsoft extensions logging?

Microsoft. Extensions. Logging can also be used for applications that don't use dependency injection, although simple logging can be easier to set up. Microsoft.

Where does the log file get written to in .NET Core logging?

For example, the Console ILoggerProvider writes the logs to the console. This interface is used to create a custom instance of an ILogger . ILoggerFactory : This interface registers one or more ILoggerProvider instances and provides the CreateLogger() method used to create an instance of ILogger .

What is the use of ILogger in .NET core?

The ILoggerFactory is the factory interface for creating an appropriate ILogger type instance and also for adding the ILoggerProvider instance. The Logging API includes the built-in LoggerFactory class that implements the ILoggerFactory interface.


2 Answers

When I create a ASP.NET 2.2 project and add the package Microsoft.EntityFrameworkCore 3.1, I get the same error.

The solution is to update all Microsoft.Extensions.Logging.* package to the version 3.1.

like image 87
vernou Avatar answered Nov 15 '22 00:11

vernou


The exception says the problem resides in 'Microsoft.Extensions.Logging.Configuration.LoggerProviderConfigurationFactory' (contained in Microsoft.Extensions.Logging.Configuration assembly).

As Microsoft says here: https://docs.microsoft.com/en-us/dotnet/api/system.methodaccessexception?redirectedfrom=MSDN&view=net-5.0

The MethodAccessException exception is thrown in situations such as the following:

  • A private, protected, or internal method that would not be accessible from normal compiled code is accessed from partially trusted code by using reflection.
  • The access level of a method in a class library has changed, and one or more assemblies that reference the library have not been recompiled.

Basically, you have a reference to Microsoft.Extensions.Logging '3.1.6', but another reference to Microsoft.Extensions.Configuration.Logging version '2.2.0', and that assembly was surely compiled before Microsoft.Extensions.Logging '3.1.6'

All can be resolved modifying/adding an explicit Package Reference in your project:

like image 25
Piero Viano Avatar answered Nov 15 '22 00:11

Piero Viano